Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle multiple inputs of a form in php

Tags:

html

forms

sql

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 ....

like image 376
Cripto Avatar asked Dec 17 '22 06:12

Cripto


2 Answers

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]}";
}
like image 98
Michael Berkowski Avatar answered Jan 02 '23 23:01

Michael Berkowski


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.

like image 44
outis Avatar answered Jan 03 '23 01:01

outis