If two fields are named people[name]
, only the last value will be sent with the form. To be completely reliable, you’ll need to use an id for each person you’re collecting details on:
<input name="people[0][name]">
<input name="people[0][email]">
<input name="people[1][name]">
<input name="people[1][email]">
...
Use an incrementing counter in your while loop to do this ($i
):
<?php
$i = 0;
while ( $posts->have_posts() ) {
...
?>
<input name="people[<?php echo $i; ?>][name]">
<?php
$i++;
endwhile;
?>
You can use the empty array key format ([]
) only if you don’t need to link other fields together.
<input name="people[name][]" type="text">
You’ll only receive non-empty field values, so empty fields will get tossed and your array key will get messed up. Consider the following situation:
<input name="people[name][]">
<input name="people[email][]">
<input name="people[name][]">
<input name="people[email][]">
If a user only filled out the second name field, the second name would have an array key of 0
. You would have no way of linking the second name to the second email.
In the following situation:
<input name="people[][name]">
<input name="people[][email]">
Each new named field will be given a unique key in the index, so the email field would be at $_POST[1][email]
.