0

This may be very obiouvs and simple, but I haven't used it, how can I get a from PHP a dynamic number of input fileds of the same type?

I have a situation where I'd like do make a dynamic number of inputs to send to the PHP page like this:

With jQuery I can add these inputs to increase or decrease the number of characters:

<!-- value is the character id -->
<tr><td><input type="checkbox" name="character" value="1256"></td></tr>
<tr><td><input type="checkbox" name="character" value="85"></td></tr>
<tr><td><input type="checkbox" name="character" value="901"></td></tr>
<tr><td>etc...</td></tr>

And I have a input submit at the end of the table to send data to my php page, how can I do to get all these characters? Should I do something directly with AJAX or can I handle it via $_POST variables?

1 Answer 1

11

Include empty array accessors in the name of your field:

<input type="checkbox" name="character[]" value="1">
<input type="checkbox" name="character[]" value="2">

Then access the post parameter as an array in php like:

foreach($_POST["character"] as $char) { … }
Sign up to request clarification or add additional context in comments.

1 Comment

It's fantastic. I love it. Thank you!

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.