0

i have a form like this

        <form action="insert_controllo.php" method="POST">
        <table>
            <th>Macchina</th>
            <th>Dispositivo</th>
            <th>Descrizione</th>
            <th>OK</th> 
            <th>NOK</th>    
            <th>PARZ</th>   
            <th>NA</th> 
            <th>Note</th>   
            <?while($row = mysql_fetch_array($risultato)){
              echo '<tr><td>  <input class="hide" type= "text" name="elements['.$count.'][macc]" value ="'.$row["macc"].'" readonly> 
                </td><td> <input class="hide" type= "text" name="elements['.$count.'][iddispsic]" value ="'.$row["iddispsic"].'" readonly> 
                </td><td> <input class="hide" type= "text" name="elements['.$count.'][descgen]" value ="'.$row["descgen"].'" readonly>
                </td><td> <input type= "radio" name="elements['.$count.'][chk1][]" value ="OK" checked>  
                </td><td> <input type= "radio" name="elements['.$count.'][chk1][]" value ="NOK">
                </td><td> <input type= "radio" name="elements['.$count.'][chk1][]" value ="PARZ">    
                </td><td> <input type= "radio" name="elements['.$count.'][chk1][]" value ="NA"> 
                </td><td> <input type="text" name="elements['.$count.'][note]" placeholder = "Note"> 
                </td></tr>';  
              $count ++;
            }?>
        </table>
        <button class="bottone" name='submit' type='submit'>Concludi</button>

in the page "insert_controllo.php" if i print_r($_POST); i can see all the variable in the right way

<!-- POST: Array
(
    [elements] => Array
        (
            [0] => Array
                (
                    [macc] => 09
                )

            [1] => Array
                (
                    [iddispsic] => 8
                    [macc] => 09
                    [descgen] => Comandi di emergenza
                    [chk1] => Array
                        (
                            [0] => OK
                        )

                    [note] => 
                )......

i'm trying to retrieve the variable like this

for($i=0; $i<count($_POST['elements']); $i++){

            $macc = $_POST['elements']['macc'];
            $dispsic = $_POST['elements']['iddispsic'];
            $descgen = $_POST['elements']['descgen'];
            $chk1 = $_POST['elements']['chk1'];
            $note = $_POST['elements']['note'];

            echo $macc;
            echo $dispsic;
            echo $descgen;
            echo $chk1;
            echo $note;

            var_dump($macc);
        }
 }

but in the var_Dump i get only NULL value. i need to INSERT them in my DB table can someone help me? thanks a lot best regards

  • 4
    Hint: `$_POST['elements'][0]['macc']`. Also, `foreach` may be more appropriate / simpler in this case. – Jonnix Mar 22 '19 at 13:33
  • 2
    You are missing `[$i]` in your references e.g. `$_POST['elements'][$i]['macc']` – Nick Mar 22 '19 at 13:34

1 Answers1

0

Try a foreach loop

foreach ($_POST['elements'] as $element) {

            $macc = $element['macc'];
            $dispsic = $$element['iddispsic'];
            $descgen = $element['descgen'];
            $chk1 = $element['chk1'];
            $note = $element['note'];

        }
 }

The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable.

http://php.net/manual/en/control-structures.foreach.php

Azael
  • 114
  • 6