Cripto
Cripto

Reputation: 3751

Handle multiple inputs of a form in php

I want to know how to handle multiple inputs from a form with multiple atributes. This code generates my fields:

<form method="POST" action="test5.php" id="1">
            <?
            if($_SESSION["peoplecount"] != 0){
            for ($i = 0; $i <= $_SESSION["peoplecount"]; $i++) {
             echo ' Name<input type="text" name="'.$i.'">  Adult<input type="radio" name="'.$i.'" value="adult" /> Minor<input type="radio" name="'.$i.'" value="minor" /> <br/>';
            }           }
            ?>
            <input class="button" type="submit" value="I/We Agree" style="width:200px;"/>
            </form>

once submitted -- or test5.php as its mentioned in the "action" part,

foreach ($_POST as $key => $value) {
  print "{$key}: {$value}<br />";
}

and the output is

0: adult
1: adult
2: adult
3: adult

Notice it has 0, 1, 2... and then adult. It does not even mention the name of the person from the text input. I can alter the form to:

        </blockquote>
        <form method="POST" action="test5.php" id="1">
        <?
        if($_SESSION["peoplecount"] != 0){
        for ($i = 0; $i <= $_SESSION["peoplecount"]; $i++) {
         echo ' Name<input type="text" name="usersname" id="usersname">  Adult<input type="radio" name="age" value="adult" id="age"/> Minor<input type="radio" name="age" value="minor" id="age"/> <br/>';
        }           }
        ?>
        <input class="button" type="submit" value="I/We Agree" style="width:200px;"/>
        </form>

Using the same test5.php, I get

usersname:
age: adult

The value of the age is not posted and the the foreach loop in test5.php ends, hence the line return, before it completly runs through one complete post.

I hope I did a good enough job explaining. I want my output to be:

SomeName Adult

SomeOtherName Minor ....

Upvotes: 4

Views: 9485

Answers (5)

outis
outis

Reputation: 77400

PHP has a special ability–if you name the inputs using array syntax, PHP will parse the input into arrays.

Also:

  • don't use short tags,
  • don't use <br/> non-semantically; instead, use paragraphs, lists or whatever is most semantically appropriate,
  • always give your inputs labels,
  • IDs must be unique

As an example of applying the above:

<?php if ($_SESSION["peoplecount"]) { ?>
  <ol>
    <?php for ($i = 0; $i <= $_SESSION["peoplecount"]; ++$i) { ?>
      <label for="name_<?php echo $i ?>">Name</label>
      <input type="text" name="name[]" id="name_<?php echo $i ?>" /> 

      <label for="adult_<?php echo $i ?>">Adult</label>
      <input type="radio" name="age[]" value="adult" id="adult_<?php echo $i ?>" selected />

      <label for="minor_<?php echo $i ?>">Minor</label>
      <input type="radio" name="age[]" value="minor" id="minor_<?php echo $i ?>" />
    <?php } ?>
  </ol>
<?php } ?>

Note that you have to be careful about using empty array brackets with certain inputs–namely, checkboxes and radio buttons–as unset inputs won't be submitted, causing the indices of the array for one set of inputs to not correspond to the indices of any other arrays. In the example above, setting a default selected radio button means exactly one will always be set. You can explicitly set the indices to prevent this:

      <label for="name_<?php echo $i ?>">Name</label>
      <input type="text" name="person[<?php echo $i ?>][name]" id="name_<?php echo $i ?>" /> 

      <label for="adult_<?php echo $i ?>">Adult</label>
      <input type="radio" name="person[<?php echo $i ?>][age]" value="adult" id="adult_<?php echo $i ?>" selected />

      <label for="minor_<?php echo $i ?>">Minor</label>
      <input type="radio" name="person[<?php echo $i ?>][age]" value="minor" id="minor_<?php echo $i ?>" />

This same technique also lets you create multidimensional keyword arrays.

Upvotes: 3

rap-2-h
rap-2-h

Reputation: 31948

You should use array :

 // [...]
 echo ' Name<input type="text" name="usersname[' . $i . ']" id="usersname" />';
 echo 'Adult<input type="radio" name="age[' . $i . ']" value="adult" id="adult" />';
 echo 'Minor<input type="radio" name="age[' . $i . ']" value="minor" id="minor"/> <br/>';
 // [...]

 for($i = 0, $count = $count($_POST['username']); $i < $count; $i++) {
      echo 'name: ' . $_POST['username'][$i]. '<br />';
      echo 'age: ' . $_POST['age'][$i]. '<br />';
 }

So $_POST['username'][0] and $_POST['age'][0] are the first user values, etc.

Upvotes: 1

nine7ySix
nine7ySix

Reputation: 461

Try modifying the form to

<form method="POST" action="test5.php" id="1">
<?
if($_SESSION["peoplecount"] != 0){
    for ($i = 0; $i <= $_SESSION["peoplecount"]; $i++) {
     echo ' Name <input type="text" name="usersname['.$i.']" id="usersname">
            Adult <input type="radio" name="age['.$i.']" value="adult" id="age['.$i.']"/>
            Minor<input type="radio" name="age['.$i.']" value="minor" id="age['.$i.']"/>
            <br/> ';
    }
}
?>
<input class="button" type="submit" value="I/We Agree" style="width:200px;"/>
</form>

Upvotes: 1

Michael Berkowski
Michael Berkowski

Reputation: 270609

Your problem is that you are creating two form inputs with name='$i', and the second one (the radio button) is overwriting the first. I would suggest instead that you use a string including $i to build the name attributes:

for ($i = 0; $i <= $_SESSION["peoplecount"]; $i++) {
    echo ' Name<input type="text" name="name-'.$i.'">  Adult<input type="radio" name="age-'.$i.'" value="adult" /> Minor<input type="radio" name="'.$i.'" value="minor" /> <br/>';
}

Now your $_POST array will look like:

name-0: somename age-0: Adult
name-1: othername age-1: Minor
...

An even better way to handle it is to use arrays as form name attributes with [] (Note I've switched to double-quotes here, to avoid all the extra concatenation and complicated quoting.)

for ($i = 0; $i <= $_SESSION["peoplecount"]; $i++) {
   echo " Name<input type='text' name='name[$i]'>  Adult<input type='radio' name='age[$i]' value='adult' /> Minor<input type='radio' name='age[$i]' value='minor' /> <br/>";
}

In this case, your $_POST looks like:

name: Array(
 0: somename,
 1: othername
),
age: Array (
 0: adult,
 1: minor
)

To access them, you can use a foreach loop like so:

foreach ($_POST['name'] as $key=>$name) {
  echo "Name: $name  Age: {$_POST['age'][$key]}";
}

Upvotes: 6

Marco Bax
Marco Bax

Reputation: 383

Try this:

<?php if (count($_POST)): ?>
  <pre>
  <?php var_dump($_POST); ?>
  </pre>
<?php endif; ?>

<form method="POST" action="test5.php" id="1">
<?
$_SESSION['peoplecount'] = 10;
if($_SESSION["peoplecount"] != 0){
for ($i = 0; $i <= $_SESSION["peoplecount"]; $i++) {
 echo ' Name<input type="text" name="name_'.$i.'">  Adult<input type="radio" name="option_' . $i . '[]" value="adult" /> Minor<input type="radio" name="option_' . $i . '[]" value="minor" /> <br/>';
}           }
?>
<input class="button" type="submit" value="I/We Agree" style="width:200px;"/>
</form>

Output can be:

  array(13) {
  ["name_0"]=>
  string(5) "Marco"
  ["option_0"]=>
  array(1) {
    [0]=>
    string(5) "minor"
  }
  ["name_1"]=>
  string(4) "SomeOtherGuy"
  ["option_1"]=>
  array(1) {
    [0]=>
    string(5) "adult"
  }
  ["name_2"]=>
  string(0) ""
  ["name_3"]=>
  string(0) ""
  ["name_4"]=>
  string(0) ""
  ["name_5"]=>
  string(0) ""
  ["name_6"]=>
  string(0) ""
  ["name_7"]=>
  string(0) ""
  ["name_8"]=>
  string(0) ""
  ["name_9"]=>
  string(0) ""
  ["name_10"]=>
  string(0) ""
}

Upvotes: 1

Related Questions