Say I have a form with 5 rows each of which contain first and last name. If each of the rows have the same name="" for the fields, how would I submit all of the values. I know that I need to use an array, I just don't know how.
Is it name="first"[]
or name="first"
What happens on the page I send it to when I do $first = $_POST['first'].
Thanks in advance for the help.
submit multiple form values with same name.
Moderator: General Moderators
Re: submit multiple form values with same name.
each input should look something like this:
<input type="text" name="firstname[]" id="firstname" />
$firstname = $_POST['firstname']; //this will be an array.
to get the values out of the array, you could use a foreach loop.
foreach($firstname as $name){
echo $name;
}
<input type="text" name="firstname[]" id="firstname" />
$firstname = $_POST['firstname']; //this will be an array.
to get the values out of the array, you could use a foreach loop.
foreach($firstname as $name){
echo $name;
}
Re: submit multiple form values with same name.
So I could have something like this
foreach($i=0;$i<=10;$i++)
{
echo "<tr>";
echo '<input name="first[]" id="first"/>';
}
then on some other page where the action takes assuming I have already connected to a database I could do something like this.
$first=$_POST['firstname'];
foreach ($firstname as $name)
{
$result=mysql_query("Insert into Person(Name) Values('$name');
}
and this would create 10 new rows in the database with the names, right.
foreach($i=0;$i<=10;$i++)
{
echo "<tr>";
echo '<input name="first[]" id="first"/>';
}
then on some other page where the action takes assuming I have already connected to a database I could do something like this.
$first=$_POST['firstname'];
foreach ($firstname as $name)
{
$result=mysql_query("Insert into Person(Name) Values('$name');
}
and this would create 10 new rows in the database with the names, right.
Re: submit multiple form values with same name.
Yes, that is correct. But for the first loop you don't use foreach. A simple for loop works. Just like you had it.
for($i=0;$i<=10;$i++){
}
this will give you 11 input fields.
And yes you are correct about inserting into the database. But check your syntax on the query. At the end of the line you are missing a ")
for($i=0;$i<=10;$i++){
}
this will give you 11 input fields.
And yes you are correct about inserting into the database. But check your syntax on the query. At the end of the line you are missing a ")
Re: submit multiple form values with same name.
Yeah my bad I meant for not foreach.
Now question is say they only fill out 5 of the ten rows. When I do the insert would it insert blank rows for the other 5 that were left blank? Or would I need some kind of statement to tell it only to insert where $first is not null or not a blank string?
Now question is say they only fill out 5 of the ten rows. When I do the insert would it insert blank rows for the other 5 that were left blank? Or would I need some kind of statement to tell it only to insert where $first is not null or not a blank string?
Re: submit multiple form values with same name.
You could wrap the insert statement in an if statement
inside the foreach loop.
foreach($firstname as $name){
if ($name){
$query....
}
}
this just checks to see if there is anything in $name.
inside the foreach loop.
foreach($firstname as $name){
if ($name){
$query....
}
}
this just checks to see if there is anything in $name.