Reputation: 3522
I have a form that looks like this:
<form method='post' action='post.php'>
<span>
<input type='text' name='ingredientQTY'/>
<select name='selectTYPE'>
<option value='1' name='1'>BLAH</option>
<option value='2' name='2'>BLAH2</option>
<option value='2' name='3'>BLAH3</option>
</select>
<input type='text' name='ingredientNAME'/>
</span>
</form>
I have a script on the side that clones the span and adds the clone to the page, so I can have n number of spans like above. I have a database called ingredients that looks like this:
Row 1: ingredientQTY -- is populated by the first input in the spans Row 2: ingredientTYPE -- is populated by the value of the select/dropdown in the spans Row 3: ingredientNAME -- is populated by the last input in the spans
So each span's fields update 1 column in a single table row. But I can have n number of rows that need to be inserted, corresponding to the number of spans that are added via jQuery.
How can I build this with PHP? I'm looking for a more conceptual answer (I don't mean for someone to build it), but examples would be nice.
Thanks for all help, and if this question was in any way confusing, just ask!
Upvotes: 0
Views: 87
Reputation: 4371
If you are processing a form using PHP you can create fieldnames like such:
<input type="text" name="ingredientQTY[]" />
or <select name="selectTYPE[]">
These names are loopable as follows:
foreach($_POST['ingredientQTY'] as $key => $value) {
$qty = $_POST['ingredientQTY'][$key]; // or $value
$type = $_POST['selectTYPE'][$key];
$name = $_POST['ingredientNAME'][$key];
}
I guess this is what you meant.
Upvotes: 2