Page 1 of 1

Form post data to database by keys

Posted: Tue Jul 07, 2009 8:11 am
by djdavedawson
:?:
I have a form that generates using a for() loop. Here is part of the code:

Code: Select all

 
$result = "one,two,three";
$items = explode(",",$result);
$num = count($items);
 
echo " <form id=\"form1\" name=\"form1\" method=\"post\" action=\"testform2.php?action=process\">
        <table width=\"50%\">";
 
for ($c=0; $c < $num; $c++) 
 
{echo  "
<tr><Name<input type=\"text\" name=\"name$c\" id=\"\" /></td>
<tr>Email<input type=\"text\" name=\"email$c\" id=\"\" /></td>
<tr>City<input type=\"text\" name=\"city$c\" id=\"\" /></td>
 
 
 
 
So i end up with input rows like this:

name0, email0, city0
name1, email1, city1
name2, email2, city2

When the form is submitted, i need the data to write to my db, where each row in the db will be data from the corresponding keys.

TIA

DD

Re: Form post data to database by keys

Posted: Tue Jul 07, 2009 10:09 am
by djdavedawson
I figured it out. :P :D

I had the $i in the wrong place

Here's what I did:

Code: Select all

  
for ($i=0; $i < $num; ++$i) 
{
$name = $_POST["name[b][color=#000000]$i[/color][/b]"];
$email = $_POST["email[b][color=#000000]$i[/color][/b]"];
$city = $_POST["city[b][color=#000000]$i[/color][/b]"];
 
$sql_data_array = array(
                  array('fieldName'=>'name', 'value'=>$name, 'type'=>'string'),
                  array('fieldName'=>'email', 'value'=>$email, 'type'=>'string'),
                  array('fieldName'=>'city', 'value'=>$city, 'type'=>'string'),
                   );
 
Thanks Everyone

DD

Re: Form post data to database by keys

Posted: Tue Jul 07, 2009 12:25 pm
by Skara
Input tags can use arrays just fine, btw:

Code: Select all

<input type="text" name="name[$c]" id="" />

Code: Select all

$name = $_POST['name'][$i];

Re: Form post data to database by keys

Posted: Tue Jul 07, 2009 3:41 pm
by djdavedawson
Thanks, I like that better, much cleaner. :P