Page 1 of 1

Trying to make a dynamic form.

Posted: Fri Sep 10, 2010 7:17 pm
by lavaeagle
I have 10 sections of a form that have 5 inputs each.
I need to label each set of 5 as a number.
Then post each into all 50 inputs into a database.

My Form:

Code: Select all

<form action="inc/frm.php?f=pro_add" method="post">
	
<table>
	<? for ($i=1; $i<=10; $i++){ ?>
		<tr>
			<td>
                        <!-- I make a list of products from a mysql db -->
			<select id="cat_id" name="cat_id<? echo $i; ?>">
				<? while ($pro4_fa = mysql_fetch_assoc($pro4)){ ?>
				<option value="<? echo $pro4_fa['cat_id']; ?>"><? echo $pro4_fa['cat_name']; ?></option>
				<? } ?>
			</select>
			<? mysql_data_seek($pro4,0); ?>
			</td>
			
			<td><input type="text" class="w95" id="pro_model" name="pro_model<? echo $i; ?>" /></td>
			<td><input type="text" class="w95" id="pro_descrip" name="pro_descrip<? echo $i; ?>" /></td>
			<td>$ <input type="text" class="w80" id="pro_sell" name="pro_sell<? echo $i; ?>" /></td>
			<td class="c"><input type="checkbox" id="tax_id" name="tax_id<? echo $i; ?>" value="1" checked="checked" /></td>
		</tr>
	<? } ?>
</table>

<input type="submit" value="Save Products" />

</form>
I need to post each of these 10 sections but I don't know how and I have been trying for literally hours with foreach, for and now I'm down to making it static just for the time being. I haven't encountered something like this before so if you can help please let me know!

Re: Trying to make a dynamic form.

Posted: Sat Sep 11, 2010 5:58 am
by oscardog
Well as long as the select input is named the same as the field in the database the following should work. Also a much better way would be to make one long query rather than a lot of small queries and then insert it but quite frankly I can't be bothered to code that out, but this should work.
foreach($_POST as $field => $value) {
mysql_query("INSERT INTO table ('".$field."') VALUES ('".$value."')");
}
[/syntax]

Re: Trying to make a dynamic form.

Posted: Mon Sep 13, 2010 2:03 pm
by lavaeagle
foreach was a farily new concept for me because the need had never come up before, but I actually found an example almost exactly like yours but each field was:

Code: Select all

<input type="checkbox" name="checkbox[]" />
<input type="text" name="item1[<? echo $item['ite_id']; ?>]
and then used a foreach that looked basically like yours, but thank you.

Re: Trying to make a dynamic form.

Posted: Mon Sep 13, 2010 2:03 pm
by lavaeagle
foreach was a farily new concept for me because the need had never come up before, but I actually found an example almost exactly like yours but each field was:

Code: Select all

<? while(something is happening){
<input type="checkbox" name="checkbox[]" />
<input type="text" name="item1[<? echo $item['ite_id']; ?>]; ?>" />
}
and then used a foreach that looked basically like yours, but thank you.