0

I am trying to pass the value of a javascript variable to a php variable.

This is my code but it's not passing anything...

 <script>
 var newVar = "2000";
 <?php $value ?> = newVar;
 </script>

Sorry, I need to javascript variable to be passing into php.

Code above turned round.

asked Feb 2, 2012 at 23:18
1
  • 2
    Do you mean PHP to JavaScript? That makes no sense for JavaScript to PHP. Commented Feb 2, 2012 at 23:21

3 Answers 3

4

This is how you pass from PHP to your Javascript. Your newVar will take the values of $value.

<script>
var newVar = "2000";
var newVar = "<?php echo ($value); ?>";
</script>

However reading again "of a javascript variable to a php variable" i see that you would like to pass a variable from javascript to php which you have several methods to do: in a form via GET or POST, in a link via GET, or via ajax.

answered Feb 2, 2012 at 23:19
Sign up to request clarification or add additional context in comments.

Comments

3

try this instead

var newVar = "<?php echo $value; ?>";

PHP doesn't emit anything unless it's been told to do so. ;)

Update

Your script is processed on the client side, after the server is done parsing the php. If you need to update some information on the server after the page has loaded on the client's browser, AJAX is the way to go.

answered Feb 2, 2012 at 23:19

Comments

0

And you must check if you are using *.php files. If not, here is solution:

For web servers using PHP as apache module:

AddType application/x-httpd-php .html .htm

For web servers running PHP as CGI:

AddHandler application/x-httpd-php .html .htm 
answered Feb 3, 2012 at 0:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.