Handle Multiple Inputs Of A Form In Php
Solution 1:
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: Adultname-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<inputtype='text'name='name[$i]'> Adult<inputtype='radio'name='age[$i]'value='adult' /> Minor<inputtype='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]}";
}
Solution 2:
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:
<?phpif ($_SESSION["peoplecount"]) { ?><ol><?phpfor ($i = 0; $i <= $_SESSION["peoplecount"]; ++$i) { ?><labelfor="name_<?phpecho$i?>">Name</label><inputtype="text"name="name[]"id="name_<?phpecho$i?>" /><labelfor="adult_<?phpecho$i?>">Adult</label><inputtype="radio"name="age[]"value="adult"id="adult_<?phpecho$i?>"selected /><labelfor="minor_<?phpecho$i?>">Minor</label><inputtype="radio"name="age[]"value="minor"id="minor_<?phpecho$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:
<labelfor="name_<?phpecho$i?>">Name</label><inputtype="text"name="person[<?phpecho$i?>][name]"id="name_<?phpecho$i?>" /><labelfor="adult_<?phpecho$i?>">Adult</label><inputtype="radio"name="person[<?phpecho$i?>][age]"value="adult"id="adult_<?phpecho$i?>"selected /><labelfor="minor_<?phpecho$i?>">Minor</label><inputtype="radio"name="person[<?phpecho$i?>][age]"value="minor"id="minor_<?phpecho$i?>" />
This same technique also lets you create multidimensional keyword arrays.
Solution 3:
Try this:
<?phpif (count($_POST)): ?><pre><?php var_dump($_POST); ?></pre><?phpendif; ?><formmethod="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/>';
} }
?><inputclass="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) ""
}
Solution 4:
Try modifying the form to
<formmethod="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/> ';
}
}
?><inputclass="button"type="submit"value="I/We Agree"style="width:200px;"/></form>
Solution 5:
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.
Post a Comment for "Handle Multiple Inputs Of A Form In Php"