Multi-page Forms with PHP
(Page 1 of 4 )
This article will touch on two skills in the interface between PHP and HTML forms, specifically in working with hidden fields, and as a bonus, in passing arrays in an HTML form.
By : Sam Fullman
This article will touch on two skills in the interface between PHP and HTML forms, specifically in working with hidden fields, and as a bonus, in passing arrays in an HTML form.
If you've been on the web at all, you've gone through multipart forms, where you fill out, let's say, section one, click submit, and fill in section two, etc.. This makes sense from dividing the forms into chunks, controlling the amount of information received, and also for verfication. If we want the user to set up an account with us, we might want to make sure that the username and password they select are unique before we have them fill out much else.
At some point in your coding career, you're going to need to use hidden fields to hold values from a previous form. For example, if Form 1 had an input field as follows:
<input type='text' name='username'> |
And you have globals turned on, Form 2 should carry this hidden field:
<input type='hidden' name='username' value='<?php echo $username;?>'> |
This will "forward" the value of the text field to the hidden field on the second form. If you have globals turned OFF, you'll need to rewrite the second field slightly:
<input type='hidden' name='username' value='<?php echo $_POST['username'];?>'> |
The concept is pretty simple; this way when the user submits Form 2, the values on form 1 go along with it. However, if you've got 5 forms with 15 questions each, form 2 will have 15 hidden fields, form 3 will have 30, form four will have 45, etc. That's a lot of hand coding, AND you'll have to change (5-n) fields if you change 1 question on Form n.
I finally came to the point where I'd had enough, and this function I'm providing was the answer. The name of the function is field_forwarder(), and it's going to be a good friend on some of the complex projects I'm about to head up. In a nutshell, it takes all of the fields in a form collection, and outputs HTML hidden fields. Please click here for the code.
Next: Using the function >>
More Miscellaneous Articles
More By Codewalkers