Jump to content

How do I handle this dynamically generated form?


poleposters

Recommended Posts

Hi,

 

I have a form that is dynamically generated. It pulls a number of items from the database and presents each one with a select menu next to it. The select menu presents a number of options that apply to each individual item.

 

ie

 

ITEM_47---<select>--

ITEM_56---<select>--

ITEM_32---<select>--

ITEM_63---<select>--

 

Now the problem I have is that if I use the ITEM_ID as the name of each field I wont be able to process the form because the process script won't know how to refer to the $_POST data in order to capture it. However if I give he fields generic names, then I won't be able to link each select menu with an ITEM_ID.

 

It seems like a pretty basic task. I'm certain there is a solution. Please let me know if I need to clarify.

 

Thanks.

Use a HTML array in your form, where the array's index is the item id and then just iterate over the array in the php code -

 

<form action="" method="post">
<select name="sel[iTEM_47]">
<option value="abc">option abc</option>
<option value="def">option def</option>
</select>
<select name="sel[iTEM_56]">
<option value="abc">option abc</option>
<option value="def">option def</option>
</select>
<input type="submit">
</form>

 

<?php
foreach($_POST['sel'] as $key => $value){
echo "Key: $key, Value: $value<br />";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.