Passing PHP var to a JS function
Discussion
I have the following:
This is fine, getting the postcode into a PHP variable.
Now I've also got a button that has an onclick event, calling a JS function. Problem is, I need to pass the $postcode value to the JS function...
Is this possible? Driving me nuts!
$postcodeSQL = "SELECT postcode FROM address";
$postcodeRes = mysql_query($postcodeSQL);
// Only one result for ease of reading (I know I need a loop and check for 0 here)
$postcode = mysql_result($postcodeRes, 0, 'pcode');
This is fine, getting the postcode into a PHP variable.
Now I've also got a button that has an onclick event, calling a JS function. Problem is, I need to pass the $postcode value to the JS function...
Is this possible? Driving me nuts!
incubus said:
I have the following:
This is fine, getting the postcode into a PHP variable.
Now I've also got a button that has an onclick event, calling a JS function. Problem is, I need to pass the $postcode value to the JS function...
Is this possible? Driving me nuts!
I'd do it using AJAX.
$postcodeSQL = "SELECT postcode FROM address";
$postcodeRes = mysql_query($postcodeSQL);
// Only one result for ease of reading (I know I need a loop and check for 0 here)
$postcode = mysql_result($postcodeRes, 0, 'pcode');
This is fine, getting the postcode into a PHP variable.
Now I've also got a button that has an onclick event, calling a JS function. Problem is, I need to pass the $postcode value to the JS function...
Is this possible? Driving me nuts!
AJAX allows you to request a URL and then process the response in JAvaScript. So you would create a php script that checks the post code. You submit that URL via AJAX and when the response comes back you can either load it directly into a JavaScript variable or update the DOM on screen to show the result.
If you google it there are loads of examples and just pick a quick and easy one.
I'm a bit busy at the moment but if you get totally stuck let me know and I'll mail you some code.
mystomachehurts said:
incubus said:
I have the following:
This is fine, getting the postcode into a PHP variable.
Now I've also got a button that has an onclick event, calling a JS function. Problem is, I need to pass the $postcode value to the JS function...
Is this possible? Driving me nuts!
I'd do it using AJAX.
$postcodeSQL = "SELECT postcode FROM address";
$postcodeRes = mysql_query($postcodeSQL);
// Only one result for ease of reading (I know I need a loop and check for 0 here)
$postcode = mysql_result($postcodeRes, 0, 'pcode');
This is fine, getting the postcode into a PHP variable.
Now I've also got a button that has an onclick event, calling a JS function. Problem is, I need to pass the $postcode value to the JS function...
Is this possible? Driving me nuts!
AJAX allows you to request a URL and then process the response in JAvaScript. So you would create a php script that checks the post code. You submit that URL via AJAX and when the response comes back you can either load it directly into a JavaScript variable or update the DOM on screen to show the result.
If you google it there are loads of examples and just pick a quick and easy one.
I'm a bit busy at the moment but if you get totally stuck let me know and I'll mail you some code.
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff


