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
?>
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
?>
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.
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
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