4

i got a list of checkbox, when i checked the checkbox, it should insert the value into database. when i unchecked the checkbox then it should be delete data from database.

if i assign a value in checkbox then i only can get the checked checkbox value. if i using a hidden field then i can get all value of checkbox but then i dunno which 1 is check and which 1 is uncheck.

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
    }
  }
?>
user367856
  • 337
  • 1
  • 6
  • 17

3 Answers3

6

The markup for a checkbox looks like so:

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

When you POST the form, you will see an array called validate in $_POST.

If that array is empty, then the checkbox was not checked. If that array contains "myvalue", then the checkbox was checked.

If you are dealing with multiple checkboxes like this:

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

Your script will need to know that the validate array in $_POST can contain values of myvalue1 and myvalue2. Then you can look into $_POST['validate'], and if the value exists in the array, then that checkbox was checked. You can use array_diff() to easily do this without writing any loops.

F21
  • 32,163
  • 26
  • 99
  • 170
2

I've found this (not semantic, but working) solution in a blog, consisting in introducing first a hidden input with the same name and the value you want to send when the checkbox is unchecked:

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

It has some drawbacks, especially if you're sending several checkboxes as an array, but does the trick for common scenarios.

msonsona
  • 1,306
  • 14
  • 20
1
<!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>