How do I submit an array of multiple input text elements?

I'm using a jQuery plugin to dynamically add input text fields. The name of the input elements becomes name="cl[]", name="ingredient[]".

Now I need to submit these fields to a database somehow...
If there was only one text field that should be added, I guess I could just do a simple foreach-bucle como este:

foreach($ingredient as $val){
    //  do an ordinary PDO sql insert statement on each of them
}

But I need is to submit two text fields, and the last inserted ID from a previous query, on each of them. If I added two fields, I would have three in total to be submitted; Like this:

<input type="text" name="oz[]" id="cl_1" placeholder="cl" class="cl" >
<input type="text" name="ingredient[]" id="ingredient_1" placeholder="ingredient name" class="ingredient" />

<input type="text" name="oz[]" id="cl_2" placeholder="cl" class="cl" >
<input type="text" name="ingredient[]" id="ingredient_2" placeholder="ingredient name" class="ingredient" />

<input type="text" name="oz[]" id="cl_3" placeholder="cl" class="cl" >
<input type="text" name="ingredient[]" id="ingredient_3" placeholder="ingredient name" class="ingredient" />

These fields are ingredients for drinks that is inserted into a sepparate table, and the last id is the relations key.

Any suggestion on how I can accomplish this?

UPDATE: I just tried to add a hidden textfield with the name raw_materials[] and do a ẁhile`-loop on that one.
Then do a sql insert statement inside this while loop. Maybe I'm on to something, but this didn't work:

while($raw_materials){
    $ins_ingredients = $con->prepare(
        'INSERT INTO recipes_ingredients (
            recipe_id, raw_material_id, amount
        ) VALUES (
            :last_id, :material, :amount
        )'
    );
    $ins_ingredients->bindValues(':last_id',$last_id);
    $ins_ingredients->bindValues(':material',$ingredient);
    $ins_ingredients->bindValues(':amount',$oz);
    $ins_ingredients->execute();
}

preguntado el 23 de agosto de 12 a las 23:08

2 Respuestas

Mira esto:

$values = array_map(null, $_POST['oz'], $_POST['ingredient']);  // You can add other arrays after this
foreach ($values as $value)
{
    list($oz, $ingredient) = $value;
    // Insert data in DB
}

Respondido 26 ago 12, 18:08

That should do the trick. I'm not able to test it out right now. But I'll test it when I get home... - TomasK

I have two tables. One for recipes with some how-to descriptions. And another table with all the raw materials needed by that recipe. I insert one record into the recipe table, which has a auto-increment, which I use to create the relation for each raw material in the other one... - TomasK

Ok then everything is fine. :) - Salvaje

The cl and ingredient field in one group have the same number, so instead of a foreach loop you should use a for loop:

for($x=0, $x<count($_POST['cl']); $x++) {
    // now you can use $_POST['cl'][$x] and $_POST['ingredients'][$x]
}

Respondido 24 ago 12, 00:08

I'm kind of stuck here. Do I first need to do an insert con el $last_id, entonces update it in order to add the cl (oz) and ingredient? - TomasK

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.