How to uncheck an uncontrolled value?

I have a list of checkboxes, when I check the box, it should paste the value into the database. when I unchecked, then I need to delete the data from the database.

if I set the value in the checkbox, then I can only get the set checkbox value. if I use a hidden field, then I can get the entire value of the checkbox, but then I do not know what 1 checks and which 1 removes.

Anyone can help?

$num="3"; for($i=1;$i<10;$i++){ ?> <form name="form1" method="post" action="testcheckbox.php"> <input type="hidden" name="task" value="validatesn"> <input type="hidden" name="validate[]" value="<?php echo $i;?>"> <input type="checkbox" name="validate[]" <?php if($num==$i){ echo "checked=checked";} ?> />Serialno<?php echo $i."<br/>"; ?> <?php $i++; } ?> <input type="submit" name="submit" value="Validate" /> </form> <?php if($_REQUEST['task'] == 'validatesn'){ $valid=$_POST['validate']; foreach($valid as $v){ echo $v; //show all checkbox values //if checkbox= checked then insert value into database //if untick the checked checkbox then delete data from database } } ?> 
+4
source share
3 answers

The markup for the flag looks like this:

 <input type="checkbox" value="myvalue" name="validate[]"> 

When you post the form, you will see an array named validate in $_POST .

If this array is empty, the check box is not selected. If this array contains "myvalue", then the checkbox has been checked.

If you are dealing with multiple flags, for example:

 <input type="checkbox" value="myvalue1" name="validate[]"> <input type="checkbox" value="myvalue2" name="validate[]"> 

Your script should know that the validate array in $_POST can contain the values myvalue1 and myvalue2 . Then you can look at $_POST['validate'] , and if the value exists in the array, then this check box is selected. You can use array_diff() to easily do this without writing any loops.

+6
source

I found this (not semantic, but working) solution on the blog consisting in first entering the hidden input with the same name and the value that you want to send when the box is unchecked:

 <input type="hidden" name="box1" value="0" /> <input type="checkbox" name="box1" value="1" /> 

It has some drawbacks, especially if you send multiple flags as an array, but a trick for ordinary scripts.

+2
source
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <form name="chk" action="" method="post"> <?php $num=2; for($q=0; $num>$q; $q++) { ?> <input type="text" name="fruits[<?php echo $q; ?>][a]" > <input type="text" name="fruits[<?php echo $q; ?>][b]"> <input type="text" name="fruits[<?php echo $q; ?>][c]"> <input type="hidden" name="fruits[<?php echo $q; ?>][d]" value="0" /> <input type="checkbox" name="fruits[<?php echo $q; ?>][d]" value="1" /> <input type="hidden" name="fruits[<?php echo $q; ?>][e]" value="0" /> <input type="checkbox" name="fruits[<?php echo $q; ?>][e]" value="1" /> <br /> <?php } ?> <input type="submit" name="ok" value="ok" /> </form> <?php if(isset($_POST['ok'])) { foreach($_POST['fruits'] as $company=>$row){ foreach($row as $fruit){ echo $fruit; } } } ?> </body> </html> 
+1
source

Source: https://habr.com/ru/post/1403192/


All Articles