Passing PHP var to a JS function
Passing PHP var to a JS function
Author
Discussion

incubus

Original Poster:

8,896 posts

313 months

Wednesday 29th August 2007
quotequote all
I have the following:

$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

Original Poster:

8,896 posts

313 months

Wednesday 29th August 2007
quotequote all
Ignore me, got round it by using echo and escape characters.


echo "<input class=\"btn_submit\" name=\"showMap\" type=\"button\" value=\"Show map\" onclick=\"load('" . $postcode . "')\" />";

mystomachehurts

11,669 posts

281 months

Wednesday 29th August 2007
quotequote all
incubus said:
I have the following:

$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!
I'd do it using AJAX.

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.

incubus

Original Poster:

8,896 posts

313 months

Wednesday 29th August 2007
quotequote all
mystomachehurts said:
incubus said:
I have the following:

$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!
I'd do it using AJAX.

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.
Thanks for that. Had thought about Ajax, but went for the simpler (in the end!) solution. Cheers.