Page 1 of 1

dynamic form from mysql

Posted: Mon Feb 28, 2011 9:09 pm
by mwarren77
I need to create a form from mysql to list a series of usernames and a few text fields for each. Then process them to a table with the results. I can do this creating an individual form for each user but thats not efficient. Here is a sample layout.
form layout
row1 {userid = hidden, Name = label, textfield, anothertextfield }
row2 {userid = hidden, Name = label, textfield, anothertextfield }
row3 {userid = hidden, Name = label, textfield, anothertextfield }
row4 {userid = hidden, Name = label, textfield, anothertextfield }

process each one with there own insert
insert into ~~~~ userid, textfield, anothertestfield

not sure how else to explain it. I thought about dynamically joining the text field like userid_textfield, userid_anothertextfield but that seems cumbersom.

There could be as few as 2 or 3 and as many as 100 users to process. Any help and examples would be much appreciated.

Re: dynamic form from mysql

Posted: Mon Feb 28, 2011 10:17 pm
by mwarren77
I think I figured this out, please let me know if this is the best practice:

<td width=10><input name="userID[]" type="checkbox" value="'.$row['userID'].'" /></td>
<td>'.$row['lname'].', '.$row['fname'].'</td>
<td><input name="textfield[]" type="text" value="0" size="10" /></td>
<td><input name="anothertextfield[]" type="text" value="0" size="10" /></td>

then parse with something like:
$i = 0;
foreach($_POST['userID'] as $key => $var){
//do insert or whatever here
echo $_POST['userID'][$i].' - '.$_POST['textfield'][$i].' - '.$_POST['anothertextfield'][$i].'<br />';
$i++;
}

I think this will get the job done. If it does I hope it helps others as well.