Reputation: 1682
Lets say I have something like this: http://gyazo.com/642987562175afc6d11a962762327744.png?1329570534
Basically, the user fills out this form, and if there are more people, pressing tab on the last form will add another input. I believe Quizlet does something similar.
How would I parse all these inputs with PHP? I obviously don't know how many inputs there will be.
Cheers
Upvotes: 0
Views: 2175
Reputation: 131931
Just create every (similar) element of the form (in html) as an entry of an array (in php). See http://php.net/faq.html.php#faq.html.arrays You can just iterate over the elements then
foreach ($_POST['MyArray'] as $element) { echo $element; }
Just to make it complete: If you click on the link above then you see, how your form elements should look like.
<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />
Upvotes: 4
Reputation: 3146
Make sure each input has a unique name by incrementing it for each additional field, eg:
<input type="text" name="field-1" />
<input type="text" name="field-2" />
<input type="text" name="field-3" />
Then when you submit it to your php script you have access to all form fields in the $_GET or $_POST arrays, depending on your form submission method.
You can do a simple while loop to iterate over $_GET['field-1'], $_GET['field-2'], $_GET['field-n'].
Make sense?
Upvotes: 0
Reputation: 2815
Variable-length argument lists
PHP has support for variable-length argument lists in user-defined functions. This is really quite easy, using the func_num_args(), func_get_arg(), and func_get_args() functions.
No special syntax is required, and argument lists may still be explicitly provided with function definitions and will behave as normal.
http://php.net/manual/en/functions.arguments.php :)
Upvotes: -1