I’m using radio buttons (‘countries’) to open dropdown-box of its corresponding ‘cities’.
The variable ‘radioPressed’ detects the selected radio button and I need to pass its value inside my JavaScript to a PHP, as follows:
Since the solution can be complicated, I would ask to receive an advice only regarding the main steps of how to pass the value of 'radioPressed' into $what_is_myVar
Here's an example. I've used "x" instead of radioPressed for the variable.
Code:
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Title</title>
<meta name="language" content="en" />
<script type="text/javascript">
//<![CDATA[
function passVariable(){
// set your variable
var x = '123';
// get the current url and append your variable
var url = document.location.href + '?x=' + x;
// to prevent looping, check to make sure your current url contains a common string
var exists = document.location.href.indexOf('?x=');
if(exists < 0){
// redirect passing your variable in
window.location = url;
}
}
//]]>
</script>
</head>
<body onload="passVariable()">
<?php echo $_REQUEST['x']; ?>
</body>
</html>
I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!
Thanks very much for your professional reply, works perfect but I need to have the PHP variable inside JavaScript, because the PHP code for opening the dropdown-box of matching cities is inside the JavaScript (see below my working script):
Bookmarks