Satch3000
Satch3000

Reputation: 49384

Passing Javascript variable to PHP not working

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.

Upvotes: 0

Views: 294

Answers (3)

Fedya Skitsko
Fedya Skitsko

Reputation: 337

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 

Upvotes: 0

Shad
Shad

Reputation: 15451

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.

Upvotes: 3

Mike
Mike

Reputation: 3024

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.

Upvotes: 4

Related Questions