Tuesday, June 23, 2009

How to pass variables between HTML documents

The most common way is using the HTTP GET method, which is the default method when you just link to a page using (a href). The disadvantage is that your variables end up visible in the URL such as in www.google.com/search?oq=variable which can be messy and poses some security issues. Also it is sometimes difficult to dynamically change your established (a href) links with user input without a LOT of javascript code. Whatever you pass ends up in receiving document in the PHP $_GET[] array.

The next most common way is by using the HTTP POST method. However, this automaticaly POSTS all of the data in all of the input fields at once, without allowing you to specify priority based on client actions. Results end up in the $_POST[] array.

My preferred method is to set up a bunch of hidden input fields, like (input type="hidden" /) and then use JavaScript events such as onFocus, onBlur, or onClick to modify the contents and the priority of these fields. Then use the standard POST method and the contents of all these fields pops up in the next document in the PHP $_POST[] array. And you can easily debug by temporarily making these fields visible (type="text") and you can see how the javascript events modify your variables as you go in real time.

No comments:

Post a Comment