-1

I've tried to get those checkbox that are unchecked. At this moment i'm just getting those that are checked. Here is my code. That value will be inserted on a table which i don't want to leave it null, that's why i need to get those checkbox unchecked, so that i could insert 1 for checked or 0 for unchecked:

  <?php 

    if (!empty($_POST['menu'])) {
    # code...
             foreach ($_POST['menu'] as $value) {
    # code...
              echo "<p>ID Checkbox checked:  ".$value;
             }
    }

   ?>

One of the reasons for what i need to get both status: checked or unchecked is because i don't want to leave database fields empty.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Alejo_Blue
  • 603
  • 3
  • 12
  • 25
  • possible duplicate of [How get value for unchecked checkbox in checkbox elements when form posted?](http://stackoverflow.com/questions/19239536/how-get-value-for-unchecked-checkbox-in-checkbox-elements-when-form-posted) – Jay Blanchard Dec 19 '14 at 21:33

5 Answers5

2

Unchecked checkboxes don't get POSTed. If you know what fields should be there, you'll probably have to list them out manually.

mopo922
  • 6,293
  • 3
  • 28
  • 31
1

The wonderful thing about checkboxes is that when you don't have one checked, and you submit a form, nothing is sent to the server. Take this checkbox for example:

<input type="checkbox" name="test"/>

If you left it unchecked and looked for $_POST['test'] it would error out, as it is not set.

So, try doing this:

if(!isset($_POST['name_of_checkbox'])){
  // Fill database with some empty value.
} else {
  // Do what you need to do for checked value.
}

Hope that gives you some insight!

Tim Lewis
  • 27,813
  • 13
  • 73
  • 102
  • thank you so much for taking the time to answer it. The problem of the solution you gave me is that i do really need the name of the checkbox unckecked that way i can insert a 0 that field like this: $inserta_menus = mysql_query("INSERT INTO menus_usuarios(id_menu, id_usuario, estado) values ('$id_menu', '$id_user[0]', '$activo')"); Where id_menu is the name of the $_post['menu'] variable – Alejo_Blue Dec 19 '14 at 21:50
  • Ah I understand. In that case, look at Artur's answer. Using a hidden element that mimics the status of the checkbox and sending that to the `$_POST[]` array instead of the checkbox is a good solution. – Tim Lewis Dec 19 '14 at 21:54
  • No Problem. Also, consider radio buttons, one for `Yes` and one for `No` if you need something to always be passed. A checkbox is essentially a radio button that can be deselected. – Tim Lewis Dec 19 '14 at 21:57
1

Say if they are named the same, and you know the number of checkbox you can use a for loop:

if (!empty($_POST['menu'])) {
    for ($i=0; $i < 10; $i++) {
        if(isset($_POST['menu'][$i])){
            echo "Checkbox checked:  " . $_POST['menu'][$i];
        }else{
            echo "Checbox uncheck #" . $i;
        }
    }
}

You can put the names in an array, then iterate:

$checkboxes = array('cbx1', 'cbx2', 'cbx3');
foreach ($checkboxes as $checkbox) {
    if (isset($_POST[$checkbox])) {
        echo "<p>ID Checkbox checked:  " . $_POST[$checkbox];
    } else {
        echo "Checbox uncheck :" . $checkbox;
    }
}

So yeah many ways to achieve this, it depends on the situation. Check for @Artur approach as well for client side solution.

meda
  • 45,103
  • 14
  • 92
  • 122
  • Hello @meda for your time sharing for your knowledge. I have tried doing your second way but this is not working though. Thank you for sharing – Alejo_Blue Dec 19 '14 at 22:45
  • @Alejo_Blue it would be helpful if you tell me what didnt work, this way I can get back to you with a solution – meda Dec 19 '14 at 23:14
  • Thank you! although nothing is shown when i open that link :( thank you though – Alejo_Blue Dec 22 '14 at 20:46
0
<input type="hidden" name="checkbox[8]" value="0">
<input type="checkbox" name="checkbox[8]" value="8">

This would be one way to also get 0's posted. Important part is that the name's are the same. So if checkbox is checked, it's value would override the hidden input value.

Artur K.
  • 3,263
  • 3
  • 38
  • 54
0

Thank you all for your time and be so kind to answer my question. The reason for what i needed to have those checkboxes unchecked and checked is because i have a dynamic menu and so i assign menus to users. Here is the solution i found with a friend of mine at work:

Code for the checkboxes. I query all the menus available on my table Menus so that i will show the administrator all the menus availables so then he could choose which menus he will assign to the users:

´ 
  <table>
  <tr>
  <td>Menus a asignar:</td>
  <td>
  <?php
  $query_menus_checkbox = mysql_query("SELECT id_menu, nombre_menu FROM menus");
  while ($checkbox_mostrar = mysql_fetch_row($query_menus_checkbox)) {
  # code...
  ?>
  <input type="checkbox" name="menus[<?php echo $checkbox_mostrar[0]; ?>]" value="<?php echo     $checkbox_mostrar[0] ?>"><?php echo $checkbox_mostrar[1] ?> 
  <p></p>
  <?php
  }
  ?>
  </td>
  </tr>
  </table>

´

Then here is the code to process which checkboxes are checked or not to insert on my table (id_user is not shown but i have taken from another query which is not shown so you'll have to query yourself):

´
 $res=mysql_query("select id_menu from menus");
                    $arid=array();
                    while($xd=mysql_fetch_assoc($res)){
                        $arid[]=$xd['id_menu'];
                    }

                    for($i=0;$i<count($arid);$i++){
                        $id_menu=$arid[$i];
                        $activo=(isset($_POST['menus'][$id_menu]) && $_POST['menus'][$id_menu]!="")?1:0;
                        $inserta_menus = mysql_query("INSERT INTO menus_usuarios(id_menu, id_usuario, estado) values ('$id_menu', '$id_user[0]', '$activo')"); 
                    }
´
Alejo_Blue
  • 603
  • 3
  • 12
  • 25