How to create an array with <input> data with checkbox checked?

Asked

Viewed 4,475 times

2

With this script, written summarized because you know better than me.

<?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++;
?>

got that:

<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 place 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 at 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?

  • Can explain better?

  • my doubt eh: which name and value should I assign to checkbox and how to write the php script to create the array with the data of each checkbox marked? Currently, I get an array with ALL data, even if I haven’t checked the checkbox.

1 answer

3

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

That is to say:

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

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

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

  • Sergio, did you mean something? <input type="hidden" value="1111" name="item[0][num_compra]" /> <input type="hidden" value="aaaa" name="item[0][plano]" > <INPUT TYPE="CHECKBOX" NAME="checkbox[]" VALUE="qualquer"> CHECKBOX 1.... a print_r in $chgeckboxes gave me a result like this: Array ( [0] => any)

  • I’m struggling to format the text

  • @Claudionishi take a look here: http://sergiofrilans.se/temp/test.php. and to format HTML here use plicas or --> ` <-- and no spaces before or after.

  • thanks, @Rgio... unfortunately I could not make it work, keeps coming all inputs. I solved doing with GET even. As this is a page where only Adm access, I think will not give problem, taking due precautions in the script.

  • @Claudionishi saw my example link? the link does what you’re looking for?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.