user2186504
user2186504

Reputation: 99

Group multiple inputs to $_POST

I can't figured out how to resolve this script: At the point 1 I need to have one $data array with one ingredient, amount and um

Here is the html part:

...
<tr>
  <td><input name="cantitate[1]" class="cantitate" /></td>
  <td><select name="unitate[1]"><?=$display->selectAmount()?></select></td>
  <td><input name="ingredient[1]" class="ingredient" /></td>
</tr>
...

Here is the php part:

// ingredients
$ingredients = $_POST['ingredient'];
$amount = $_POST['cantitate'];
$um = $_POST['unitate'];

foreach($_POST as $key => $value){ 

 $data = null;
 if(isset($_POST['ingredient']) && !empty($_POST['ingredient']) && $_POST['ingredient']!=''){

    if($key=='cantitate') $data['amount'] = $value;
    if($key=='unitate') $data['um'] = $value;
    if($key=='ingredient') $data['ingredient'] = $value;                    

    //var_dump($data);
    // point 1
    // here will be the function to add one ingredient with one um, and one amount
 }

}

Thanks!

Upvotes: 2

Views: 1517

Answers (2)

ex3v
ex3v

Reputation: 3566

I think you should try making

<tr>
  <td><input name="entry[1][cantitate]" class="cantitate" /></td>
  <td><select name="entry[1][unitate]"><?=$display->selectAmount()?></select></td>
  <td><input name="entry[1][ingredient]" class="ingredient" /></td>
</tr>

where 1 comes from interator.

Then you can get one big array with all the requests using

$data = $_POST['entry']

and iterate thru it.

Upvotes: 2

Oliver Spryn
Oliver Spryn

Reputation: 17358

You could do this, if I understand you correctly:

<tr>
<td><input name="data[]" class="cantitate" /></td>
<td><select name="data[]"><?=$display->selectAmount()?></select></td>
<td><input name="data[]" class="ingredient" /></td>
</tr>

and for the PHP:

<?php 
$data = $_POST['data']);
print_r($data); //$data[0] == cantitate, $data[1] == unitate
?>

Upvotes: -1

Related Questions