web site hosting

JavaScript Variables to PHP

PHP
First, why would you want to pass JavaScript Variables to a PHP script? After all PHP has functions that cover most eventualities without resorting to JavaScript. One good reason would be to read and pass information from JavaScript cookies to PHP. There's no direct way to interface JavaScript with PHP, however, HTML does interface with PHP via CGI and one way to pass variables from an HTML page to a PHP script is by using a query string. This takes the form

<a href="another_page.php?name=Dave&number=100">another page</a>

If a user clicks on this link, the variables $name and $number are passed to the PHP script another_page.php. The problem here is that the variables $name and $number aren't actually variables since their values were written as fixed values into the HTML page, making them more like constants. However, this is a start and we can build on it since we can use JavaScript to output HTML using the document.write method. So the link above could be written as

<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
<!-- //
document.write('<a href="another_page.php?name=Dave&number=100">another page</a>');
// -->
</SCRIPT>

This gives the same result as the HTML link so still doesn't do what we want, however, the document.write method allows concatenation which permits JavaScript variables to be embedded within strings. Therefore we can use this to write JavaScript variables into a query string and pass them to a PHP script as shown below.


<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
<!-- //
var name="Dave";
var number=100;
document.write('<a href="another_page.php?name='+name+'&number='+number+'">another page</a>');
// -->
</SCRIPT>

This now passes the variables as required when a user clicks on the link.

This method works but there are two important points to note. The first is that query strings can produce unforeseen results if certain characters are used i.e. characters that are reserved as delimiters. Therefore if you're not sure what value a string variable can take then you should have code in place to check it carefully before it's passed, especially if the string is gathered from user input. The second point is that you can't use the method to pass JavaScript variables to a PHP script in the same page as the JavaScript, they have to be passed to a PHP script on another page. The reason is that PHP is parsed server side before the page is downloaded and JavaScript is parsed client side after the page is downloaded. The variable values have to be written before they're passed and this wouldn't happen if you tried to write them directly to PHP in the same page, since the PHP would be parsed before the JavaScript.


Advertisement


© 2000-2008 smallbizonline website design Tel: 01501 771106 Privacy Policy  Terms & Conditions    RSS Feeds
We accept Visa, Mastercard, Electron, Delta, Maestro, Link, Amex, Solo and Paypal

Reviewed and approved by the Good Net Guide UK Database Driven Web Designers
JavaScript to PHP variables
Professional Web Design Services



01501 771106