Topic: Form values into an indexed array

I'm wanting to create a form with several rows to send out invitations.
e.g.
Name: "value"  Email: "value"
Name: "value"  Email: "value"
Name: "value"  Email: "value"

How would I code this to place the form values into an array? Would something like this work?
<form>
<table>
<?php
"<tr>  // (note: this row would be generated using a for loop and there would be several of these table rows.)
<td><input type=\"text\" name=\"whole_name\" value=\"<?php echo $whole_name[]; ?>\" id=\"whole_name\" /></td>
<td><input type=\"text\" name=\"email_list\" value=\"<?php echo $email_list[]; ?>\" id=\"email_list\" /></td>
</tr>";
?>
</table>
</form>

Would simply placing brackets after the value variable, create an indexed array? e.g.... $whole_name[]

Vote up Vote down

Re: Form values into an indexed array

close, just added brackets [] is used for checkboxes, because you have text fields you shoud include an index inside the brackets.

//assuming you have a for loop like
for ($row=0; $row<5; $row++)
{
  echo "... name=\"whole_name[$row]\" ...";
}
 
// fyi html also likes single quotes, this just looks cleaner
 
  echo "... name='whole_name[$row]' ...";
 
// it will return
 
$_POST['whole_name'][0]

Vote up Vote down

Re: Form values into an indexed array

I do have a for loop, if it's correct. Thanks for the heads up using single quotes. Everything I've read has used double quotes for some reason. Don't I need double quotes, where there is a Variable or Array involved?   Let me try again.

<?php
// to be used in the for-loop below.
$invite_tr =
"<tr>
<td><input type='text' name='whole_name' value=\"<?php echo $whole_name[$n]; ?>\" id='whole_name' /></td>
<td><input type='text' name='email_list' value=\"<?php echo $email_list[$n]; ?>\" id='email_list' /></td>
</tr>"
;
 
// display the number of input rows for invites.
for ( $c=0, $c<$n, $c++) {
echo $invite_tr;
}
?>

Last edited by dms (February 7, 2010 11:55 pm)

Vote up Vote down

Re: Form values into an indexed array

No, you will just echo the same thing $n times. Take a look at this

<?php
// display the number of input rows for invites.
for ( $c=0, $c<$n, $c++) {
echo "<tr>
<td><input type='text' name='whole_name["
.(int) $c."]' value='".$whole_name[$c]."' /></td>
<td><input type='text' name='email_list["
.(int) $c."]' value='".$email_list[$c]."' /></td>
</tr>\n"
;
}
?>

View your html source to see the generated code.
You will have name='whole_name[0]'
Also, I removed id= because unless you're doing stuff with client side java script you don't need it, and if you do it must be a unique value (to be valid html) so you'd have to append $c to the id value

Vote up Vote down

Re: Form values into an indexed array

I removed id= because unless you're doing stuff with client side java script you don't need it

No java script in use. I've been wondering about the "id" attribute. Thanks!  Whether to use "id" or "name" has been confusing to me. Most books use "name", but one HTML book I've read suggest using "id" due to the "name" attribute being deprecated.

Vote up Vote down