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) { … }
1
  • It's fantastic. I love it. Thank you!
    – vitto
    Dec 5, 2010 at 0:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.