submit multiple form values with same name.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

submit multiple form values with same name.

Post by gammaman »

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.
User avatar
jkraft10
Forum Newbie
Posts: 10
Joined: Sun Feb 14, 2010 8:00 pm
Location: Fontana, CA

Re: submit multiple form values with same name.

Post by jkraft10 »

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;
}
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

Re: submit multiple form values with same name.

Post by gammaman »

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.
User avatar
jkraft10
Forum Newbie
Posts: 10
Joined: Sun Feb 14, 2010 8:00 pm
Location: Fontana, CA

Re: submit multiple form values with same name.

Post by jkraft10 »

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 ")
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

Re: submit multiple form values with same name.

Post by gammaman »

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?
User avatar
jkraft10
Forum Newbie
Posts: 10
Joined: Sun Feb 14, 2010 8:00 pm
Location: Fontana, CA

Re: submit multiple form values with same name.

Post by jkraft10 »

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