1

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++;
    }
Hugo Torres
  • 308
  • 4
  • 13
  • Check http://stackoverflow.com/questions/4997252/get-post-from-multiple-checkboxes out for an easier way of working.. – Naruto Nov 06 '15 at 15:26

2 Answers2

0

You don't use value checked when need

echo '<form method="POST"><input type="checkbox" 
     name="encomendar_'.$row[0].'"  checked  ></form>Encomendar';
            }
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

You've to add a value to your input

echo '<form method="POST"><input type="checkbox" name="encomendar_'.$row[0].'" value="$row[0]"></form>Encomendar';

for example.

After that, you'll be able to check if it is set :

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++;
}
Julien Bourdic
  • 1,398
  • 1
  • 12
  • 29