Question

Edited:

So, I have multiple checkboxes. My goal is to insert checkbox value in MySQL if is checked, and to delete checkbox value from MySQL if is not checked. Everything works fine except deleting the value. PHP doesn't know which checkboxes are "unchecked". Any idea?

So far I have this:

if(isset($_POST['values']))
{
    foreach($_POST['values'] as $checked)
    {
        $query5 = ("
        INSERT INTO ecust_user_contract (fk_contract, fk_cust_user)
        VALUES ('".$checked."','".$username_u."')
        ");
        $result5 = mysqli_query($conn,$query5); 
    }
}
if(!isset($_POST['values']))
{
    foreach($_POST['values'] as $unchecked)
    {
        $query5 = ("
        INSERT INTO ecust_user_contract (fk_contract, fk_cust_user)
        VALUES ('".$unchecked."','".$username_u."')
        ");
        $result5 = mysqli_query($conn,$query5);
    }
}
Was it helpful?

Solution 2

Assuming that you have used POST method for form submission, you can do something like this:

html:

<input type="checkbox" name="values[]" value="val1">
<input type="checkbox" name="values[]" value="val2">
<input type="checkbox" name="values[]" value="val3">

php:

if(isset($_POST['values'])) {
 foreach($_POST['values'] as $checked){
 mysqli_query("insert into tablename(value) values($checked)");
  }
 }else{
 // use Delete query
  }

OTHER TIPS

This is my checkbox:

<input type='checkbox' name='Event' value='CB1' />

Where you process the form, to read this you do:

$event = $_POST['Event'];

this gets the value of the checkbox:

echo $event;

But make sure its set or you will get a null value.

If you have several of them you can do this:

<input type='checkbox' name='Event[]' value='CB1' />
<input type='checkbox' name='Event[]' value='CB2' />

$event = array_values($_POST['Event']);

And to get the values for each one, put this in a for loop:

for ($val = 0; $val < count($event); $val++)
        {
            //do something
            echo $event[$val];
        }

Try this code:

<input type='checkbox' name='Event[]' value='CB1' />
<input type='checkbox' name='Event[]' value='CB2' />

Use multiple checkboxes with array like this in your form.

after submit the form use the following to check whether checkbox(es) checked or not.

<?php
         if(isset($_REQUEST['Event'])) 
         {
                // do what you want for checked the boxes
         }
         else
         {
                // do what you want for not checked any box
         } 
?>

- Thanks

I suppose you are submitting some html form to server side code in php. If you html page looks like:

<input type="checkbox" name="test" value="value1">

After submitting the form you can check it with:

isset($_POST['test'])
or
if ($_POST['test'] == 'value1')

Suppose your HTML code for checkbox is like this:

<input type="checkbox" name="abc" value="yes" />

Then on php side:

<?php
    if( isset( $_POST['abc'] ) ) {
        --Connect to your database--
        --write your query for Insertion--
        }
        else {
        --Connect to your database--
        --write your query for Deletion--
        }
    ?>

Work for me, save both check and uncheck value to mysql

HTML:

<form method="post" action="jawab25.php">
<input type="hidden" name="check_lista[]" value="<?php echo $r["id"]?>">
<input type="checkbox" name="check_listb[]" value="<?php echo $r["id"]?>">
<input value="Check" type="Submit">

jawab25.php :

    foreach($_POST['check_lista'] as $itema){
        $string="update trans set ck='N' where id='$itema'";
        $tampil=mysql_query($string);
    }

    foreach($_POST['check_listb'] as $itemb){
        $string="update trans set ck='Y' where id='$itemb'";
        $tampil=mysql_query($string);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top