tags:

views:

51

answers:

2

Hi

I have this code for example :

$b = "";
 while ($row = mysql_fetch_array($rows)) {
        if ($row['enabled'] == 1) {
                    $b = "checked";
              } else {
                     $b = "":
                     }

echo "<\input name='nam[$row[id]]' type='checkbox' value='$row[id]' $b />";

}

When I execute this code, I will get a list of checkboxes, some of them are checked and others are not.

I can use this code to get a list of checked checkboxes.

 if (isset($_POST['sub'])) { //check if form has been submitted or not
$nam = $_POST['nam'];
if (!empty($nam)) {

              foreach($nam as $k=>$val){

    // proccess operation with checked checkboxes
               }

}           

I need to know how I can get list of unckecked checkboxes after submitting the form.

Thanks in advance.

A: 

Browsers send nothing for unchecked boxes, so your only hope is creating a hidden field that tells you the name of each checkbox (or add a hidden field for each checkbox).

barrycarter
I don't think create hidden fields is good idea because what if I have 100 rows in my db, I will need a 100 hidden fields!!!
SzamDev
Then create one hidden field that contains all the checkboxes/ids, separated by commas.
barrycarter
After I create this hidden field with all checkboxes/ids, separated by commas. How I can know the unchecked ones.
SzamDev
You split that field in the POST part of your form and subtract the list of checked boxes from the whole list.
barrycarter
A: 

If the check boxes are dynamically created this is a trivial task:

Create a naming convention. For example, lets say that each checkbox is tied to a row in the DB, each row in the DB has a primary key. You can name each checkbox based off this key:

<input type="checkbox" id="foo_1" name="foo_1" />
<input type="checkbox" id="foo_2" name="foo_2" />
<input type="checkbox" id="foo_3" name="foo_3" />

Now when the form is submitted you can query the database to get the Id's and process the request as follows:


$res = mysql_query("select * from foo;");
while($row = mysql_fetch_array($res))
    $checked = isset($_POST["foo_{$row['id']}");
    ...
}

mmattax