2

I have a form that includes the first name and last name of a person. The user can add multiple people using a link, that creates new input fields via JS. Here's an example of a form that includes 2 people:

<form action="" method="post">
    <input type="text" class="required" name="people[first][]" />
    <input type="text" class="required" name="people[last][]" />

    <input type="text" class="required" name="people[first][]" />
    <input type="text" class="required" name="people[last][]" />

    <input type="submit" name="submit">
</form>

I'm trying to figure out a way to insert this data into the database. I've tried using:

foreach ($_POST['people'] as $person) {
  foreach ($person as $value) {
    echo $value . '<br/>';
  }
}

.. which results in

first name 1
first name 2
last name 1
last name 2

I'm trying to group the results somehow so I can insert a new row for each first name x + last name x combination.

1
  • 1
    Why would you not set your people value like people[0][first]? then each array element would correspond to a row. Commented Jun 12, 2012 at 17:44

2 Answers 2

4

Create the input elements like this:

<input type="text" name="people[0][first]" />
<input type="text" name="people[0][last]" />
<input type="text" name="people[1][first]" />
<input type="text" name="people[1][last]" />

In your PHP:

foreach ($_POST['people'] as $person) {
  echo $person['first'].' '.$person['last'].'<br />';
}
Sign up to request clarification or add additional context in comments.

Comments

1

$_POST['people']['first'] is an array of first names.
$_POST['people']['last'] is an array of last names.

You can merge them into an array of arrays like this:

$people = $_POST['people'];
$length = count($people['first']);
for($i = 0; $i < $length; $i++)
    $temp[] = array('first' => $people['first'][$i], 'last' => $people['last'][$i]);
$people = $temp;

The resulting array in $people will be an array of associative arrays, and might look like:

Array
(
    [0] => Array
        (
            [first] => Jim
            [last] => Smith
        )

    [1] => Array
        (
            [first] => Jenny
            [last] => Johnson
        )

)

which is equivalent to the array you would get by modifying your HTML as bsdnoobz has shown you can do as well. Iterating through it would be the same too:

foreach ($people as $person) {
    echo $person['first'] . ' ' . $person['last'] . '<br />';
}

3 Comments

@JeremyHolovacs This way you have information about each person grouped together. It builds exactly the array that bsdnoobz has made, but it doesn't require modifying the HTML, which may be better suited for the OP.
I wonder what would happen if one of the fields was blank. Would that screw up the synchronicity? This seems to be creating an awkward workaround for a bad design.
@JeremyHolovacs No, blank fields still get posted they just contain the empty string. IMHO putting indices in the HTML is the work around, as it could be less maintainable. If you want to add an earlier entry to the form you would have to manually modify all the indices increasing them by 1.

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.