-2

I want to access javascript variable in php, how can I can access in simple way? consider example

<script "text/javascript"> 
    var a = 10; 
</script>
<?php
    echo a; // here **a** is javascript variable
?>
Cœur
  • 37,241
  • 25
  • 195
  • 267

3 Answers3

1

You mixed both server side and client side code here. If you need to echo a javascript variable in PHP then send that variable to php file and then you can echo that variable.

I hope this helps you.

Sourabh Kumar Sharma
  • 2,864
  • 3
  • 25
  • 33
0

The most easy way to do this, pass the variable in an URL parameter and $_GET it in php;

window.location.href=index.php?var=+var+

$_GET['var']

Or, post it to your PHP file using AJAX! Maybe you could fiddle around with JSON, but I recommend reading into AJAX;

http://api.jquery.com/jquery.ajax/

I've done it a few times just by 'posting' the variables to my script

0

There is two famous ways to do it: $_GET & $_POST. Both returns array of different members to the server.

$_GET is easier than $_POST.

for example, if you want to pass the variable a to the php script, you must make the header of your page look like www.website.com/index.php?a=10. So now the array of $_GET has a as a member and it's value is 10.

in your php side to call the a variable from the header and print it :

echo $_GET['a'];

refrence here

Abozanona
  • 2,261
  • 1
  • 24
  • 60