Multi-page Forms with PHP - Using the function
(Page 2 of 4 )
How to use the function:
1. The basic use couldn't be more simple. Somewhere in your second form, (best near the top before other fields), just add this code:
<?php echo field_forwarder();?> |
You'll get hidden fields printed out for every form element that was passed. if you have form elements that are ARRAYS, it works for those too.
2. Use other sources for field_forwarder()
What the function is looking at is the $_POST collection, which is an array. You can also use other arrays in place of this. Suppose you had an included file with the following array declared:
<?php $info['residence']['recent'] = "123 Main Street."; $info['residence']['previous'] = "1044 S. Fedora Drive"; $info['interests'][1] = 'hockey'; $info['interests'][2] = 'figure skating'; $info['name'] = 'Samuel "Sambo" Fullman'; $info['company'] = 'Compass Point Media'; ?> |
To get this into fields, we'd just declare the following after the array has been declared:
<?php echo field_forwarder('info');?> |
And instead of the form collection, it'll output the $info array as follows:
<input type="hidden" name="info[residence][recent]" value="123 Main Street."> <input type="hidden" name="info[residence][previous]" value="1044 S. Fedora Drive"> <input type="hidden" name="info[interests][1]" value="hockey"> <input type="hidden" name="info[interests][2]" value="figure skating"> <input type="hidden" name="info[name]" value="Samuel &quot;Sambo&quot; Fullman"> <input type="hidden" name="info[company]" value="Compass Point"> |
Two things to note here: 1) When you declare arrays in HTML form elements, you DON'T put quotes around string indexes, 2) the quotes for "Sambo" were replaced with ", otherwise the browser wouldn't know where the value ends.
My function handles arrays quite handily. In fact there is no real limit on the number of keys (indexes) that can be used. If you're actually strange enough to have an array element called:
<?php $animals['cordates']['vertebrates']['mammals']['quadripeds']['canines']['Great Danes']['my pet'] = "Rover" ?> |
it's not a problem.
3. Output PHP code instead of hidden fields
This function can also output the code for a PHP array instead of hidden fields. Just add the following before you declare the function:
<?php $FFoutputType ='print'; #must be lowercase print echo field_forwarder(); ?> |
This is great if you want to store PHP-style arrays in a database to be evaluated later on.
Next: Examining the Function >>
More Miscellaneous Articles
More By Codewalkers