1

With this line of code, I add every selected checkbox's value into the database:

    // ADD ALL TYPES TO PRODUCTIONLOG_TYPE TABLE
    $x=1;
    $values=array();
    foreach($_POST['id'] as $x)
    {
        $AddToListQuery = "
                        INSERT INTO
                        productionlog_type
                       (productionlogid, typeid)
                        VALUES
                       ('" . mysql_real_escape_string($_GET['productionlog']) . "', '". mysql_real_escape_string($x) ."')
                       ";
        mysql_query($AddToListQuery)or die("query fout " . mysql_error() );
    }

I do this so I can echo out the checkbox checked if this is alreayd in the database.

The problem now is, when the user unchecks the checkbox and sends the form, it's not passing any value, right? So I can't delete it from the database...means that the checkbox remains checked.

What can I do about this?

4
  • 2
    You should know which checkboxen you put in the form. Every checkbox that was not checked is not in the returned data. Diff the checkboxen that you put in the form and the ones that came back and unset the difference in the database. Commented Dec 18, 2012 at 11:18
  • Please use PDO instead of standard mysql_ methods Commented Dec 18, 2012 at 11:19
  • true. So I tried foreach (!isset($_POST['id'] as $x)) but that returns me an error...not really good with these arrays Commented Dec 18, 2012 at 11:20
  • 1
    foreach (!isset($_POST['id'] as $x) will not work because !isset returns a boolean. And you can not foreach a boolean Commented Dec 18, 2012 at 11:22

1 Answer 1

2

If you know which of the checkboxes will be returned, you can filter out the ones you know something about and only the checkboxes that are not checked remain then.

You can also have a look at some processing of your page before you send the request, with JavaScript for example.

Or you can pass another hidden variable in your form that contains all the checkboxes you should receive in your PHP script. For example:

<input type="hidden" value="id_1,id_2,id_3" />

If you then receive this in your PHP script, you can see which of the ids (of the checkboxes) are expected.


EDIT

If you have:

<form method="POST">
    <input type="hidden" name="checks" value="id1,id2" />
    <input type="checkbox" name="id1" checked="checked" />
    <input type="checkbox" name="id2" />
</form>

And then in PHP:

$arr = split(",", $_POST["checks"]);
foreach ($arr as $x){
    if(isset($_POST[$x])){
        // Add or update
    }else{
        // Remove
    }
}

Note: I haven't tested the code, but it you just to get an idea on how it works

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm well I do already use: <input type='checkbox' name='id[". mysql_real_escape_string($typeid) ."]' value='". mysql_real_escape_string($typeid) ."'" Now I tried what you said: <input type='hidden' value='emp[".$typeid."]'; and this works with this code: // REMOVE ALL TYPES FROM PRODUCTIONLOG_TYPE TABLE $x=1; $values=array(!isset($_POST['emp'])); foreach ($values as $x) { // delete query } But only the 1st checkbox gets deleted from database, not the others
I've updated my answer, added an example. Hopefully this will make everything clear

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.