How to create an array with input data with checked checkboxes?

1

With this script, written in short because you know better than I do.

<?php
$query = "select from table where status='0'";

$i=0; 
while (mysqli_fetch_array)

echo '<input type="hidden" value="' . $num_compra . '" name="item['.$i.'][num_compra]" />
<input type="hidden" value="'.$plano.'" name="item['.$i.'][plano]">';
$i++;
?>

I got this:

<html>
<input type="hidden" value="1111" name="item[0][num_compra]" />
<input type="hidden" value="aaaa" name="item[0][plano]" >

<input type="hidden" value="2222" name="item[1][num_compra]" />
<input type="hidden" value="bbbb" name="item[1][plano]" >

<input type="hidden" value="3333" name="item[2][num_compra]" />
<input type="hidden" value="cccc" name="item[2][plano]" >

quantidade de pares de input de acordo com o banco de dados.

<input type="submit">
</html>

After SUBMIT:

echo $_POST['item'];
Array ( [0] => Array ( [num_compra] => 1111 [plano] => aaaa ) 
    [1] => Array ( [num_compra] => 2222 [plano] => bbbb )
    [2] => Array ( [num_compra] => 3333 [plano] => cccc ) )

So far so good, now the problem : I need to put a checkbox for each input

<input type="hidden" value="1111" name="item[0][num_compra]" />
<input type="hidden" value="aaaa" name="item[0][plano]" >
<INPUT TYPE="CHECKBOX" NAME="????" VALUE="?????"> CHECKBOX 1

<input type="hidden" value="2222" name="item[1][num_compra]" />
<input type="hidden" value="bbbb" name="item[1][plano]" >
<INPUT TYPE="CHECKBOX" NAME="?????" VALUE="?????"> CHECKBOX 2

<input type="hidden" value="3333" name="item[2][num_compra]" />
<input type="hidden" value="cccc" name="item[2][plano]" >
<INPUT TYPE="CHECKBOX" NAME="?????" VALUE="????"> CHECKBOX 3

and create an array only with checkbox = checked

CHECKBOX 3 checked
Array ( [0] => Array ( [num_compra] => 3333 [plano] => cccc ) )

CHECKBOX 2 and CHECKBOX 3 checked

Array ( [0] => Array ( [num_compra] => 2222 [plano] => bbbb )
        [1] => Array ( [num_compra] => 3333 [plano] => cccc ) )

CHECKBOX 1 and CHECKBOX 3

Array ( [0] => Array ( [num_compra] => 1111 [plano] => aaaa ) 
        [1] => Array ( [num_compra] => 3333 [plano] => cccc ) )

and in the end, use this array to do

UPDATE table SET status='1' WHERE num_compra = $num_compra AND plano = $plano;

What is the solution to this?

    
asked by anonymous 03.10.2014 / 04:04

1 answer

2

If you use the same name for these inputs and with [] , then in PHP you will have an array with the checkboxes that are checked.

That is:

<input type="checkbox" name="checkbox[]" value="?????"> checkbox 1

So in PHP you can use $chgeckboxes = $_POST['checkbox']; that will be an array.

The value is just to assign the value you need on the PHP side, you have to choose it. Or a boolean or for example a price or the product number. It depends on what you want.

    
03.10.2014 / 07:08