Passing JavaScript variable to PHP
In my program, radio button pressed will open its corresponding dropdown list (for example: the button ‘Asia’ will open China, Japan and India).
The variable ‘x’ below detects the selected radio button and I need to pass its value inside my JavaScript to a PHP (to open the corresponding list):
<script>
var x = 123;
<?php $what_is_myVar = x ; ?>
</script>
The following code is successfully passing the variable ‘x’ to PHP, but this is done outside JavaScript:
- <head>
<script type="text/javascript">
function passVariable(){
var x = '123';
// get the current url and append variable
var url = document.location.href + '?x=' + x;
// to prevent looping
var exists = document.location.href.indexOf('?x=');
- if(exists < 0){
// redirect passing variable
window.location = url;
}
}
</script>
</head>
- <body onload="passVariable()">
<?php echo $_REQUEST['x']; ?>
</body>
Please anyone can help me with an idea, using my code or Jquery, to pass inside JavaScript the value of 'x' into $what_is_myVar
Thanks for any reply,