Ankur
Ankur

Reputation: 169

Multiple input types with same name to be received via php

I'm developing an application where the user inputs the number of children he/she is the parent off and then he/she is taken to a page where he/she enters the values of the no. of children.

Suppose there 2 children, then there would be 2 same forms with the same id for input boxes. The question is, how can I get the values simultaneously via php? Here's the code I've written, but it doesn't seems to be working correctly.

HTML

<form action="test.php" method="post">
<table>
<tr>
<td>Name 1 </td>
<td><input type="text" name="name[]"></td>
</tr>
<tr>
<td>Name 2</td>
<td><input type="text" name="name[]"></td>
</tr>
<tr>
<td><input type="submit" value="submit"></td>
</tr>
</table>
</form>

(2 children, so there would be two names)

PHP

<?php
$names=$_POST['name[]'];
foreach($child as $names)
{
    echo $child;
}
?>

What can I do to make it work perfectly? What I actually want is, the names from both the textboxes should be available. Since we don;t know how many number of children are there, we need to have one single form which would get information for all the children.

Thanks.

Upvotes: 0

Views: 70

Answers (2)

Nes
Nes

Reputation: 591

You have the foreach wrong as well

Should be:

<?php
$names=$_POST['name'];
foreach($names as $child)
{
    echo $child;
}
?>

Upvotes: 0

Nouphal.M
Nouphal.M

Reputation: 6344

Try

 $_POST['name']

instead of

$_POST['name[]']

Upvotes: 1

Related Questions