vendredi 6 novembre 2015

PHP checkboxes with variables in name never checked

I'm developing a store site in HTML/PHP and in order for people to check the items they want to buy, I've placed a checkbox in every product, with the name being "Order_(product_id)". The thing is, when I need to check if that checkbox is selected, it never is. I'm not sure what am I doing wrong. Here's the code generating the checkboxes:

while ($i < $num_linhas) {
        $row = pg_fetch_row($result, $i);
            echo "<div class='product'>
                    <h3>".$row[2]."</h3>
                    <img src='common/images/".$tipo_produto."/".$row[1].".jpg' alt='".$row[1]."'>

            <br>";
            if($_SESSION['tipo'] == "cliente")
                {
                    echo '<form method="POST"><input type="checkbox" name="encomendar_'.$row[0].'"></form>Encomendar';
                }



            $i++;
            echo "</div>";

    }

and the code to check if they are checked (different script):

while($i < $num_linhas)
    {
        $row = pg_fetch_row($result_1, $i);

        if(isset($_POST['encomendar_'.$row[0].'']))
        {
            echo $row[0];
            $query = "SELECT * from biofood.encomenda";
            $result_2 = pg_exec($conn, $query);
            $num_linhas = pg_num_rows($result_2);
            $j =  $num_linhas + 1;
            $t = time();
            $data = date('y-m-d');
            $hour = date('H:i');
            $id_cliente = $_SESSION['id'];

            $query = "Insert into biofood.encomenda(id,data,hora,id_cliente,id_produto,estado) VALUES('".$j."','".$data."','".$hour."','".$id_cliente."','".$row[0]."','Não tratada')";
            $result_3 = pg_exec($conn, $query);

        }
        $i++;
    }

Aucun commentaire:

Enregistrer un commentaire