0

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

0

3 Answers 3

4

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[]" />
Sign up to request clarification or add additional context in comments.

2 Comments

It's worth noting that when you write the HTML for the inputs you'll specify <input name="xy[]" as the name of all the inputs to have the array created in that name and placed in the GET or POST php global array.
@itsmequinn Thats mentioned on php.net (see link). Ive added it to the answer for the case, that php.net is eventually not available anymore.
0

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?

Comments

-1

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.