Insert checkbox value with Unchecked and checked Checkbox

I have problem to insert  checkbox checked  and unchecked  value in database

I am created dynamic checkbox through php. code is below
 for($i=0; $i<count($array); $i++){
         echo "<div class='col-md-1'>"; 
         echo "<label class='control-inline fancy-checkbox custom-bgcolor-green'>";
         echo "<p> $array[$i]</p>";
         echo "<input type='checkbox' name='obj_action[]' value='$array[$i]'>";
         echo " <span></span>";
         echo "</label>";
         echo "</div>";          
        } 

Open in new window

my db is
Obj_Action , Obj_Allow, Date_time_stmap

My problem is that
I am able to insert only checked checkbox not unchecked checkbox
so any one can help me

Thank You

Regards
MOHAN SINGH
LVL 3
mohan singhWeb developerAsked:
Who is Participating?
 
Julian HansenConnect With a Mentor Commented:
When a form submits it only submits checked checkboxes. There are various ways of getting around this.

One of them is to adjust the code you have slightly to use an indexed checkbox like this

 echo "<input type='checkbox' name='obj_action[{$i}]' value='$array[$i]'>";

Open in new window

When you submit the form the unchecked checkboxes will be gaps in the array
Include on your page a hidden value that tells you how many checkboxes to expect
echo '<input type="hidden" name="cbn" value="' . count($array) . '">';

Open in new window


On the server you do this
$cbn = isset($_POST['cbn']) ? $_POST['cbn'] : 0;
$cb = isset($_POST['obj_action']) ? $_POST['obj_action'] : array();
for($i = 0; $i < $cbn; $i++) {
    $checked = isset($cb[$i]) ? $cb[$i] : 0;
    // Do you insert over here
}

Open in new window

2
 
mohan singhWeb developerAuthor Commented:
Julian Hansen Sir,

yes i am inserting data under for loop
$cbn = isset($_POST['cbn']) ? $_POST['cbn'] : 0;
$cb = isset($_POST['obj_action']) ? $_POST['obj_action'] : array();
for($i = 0; $i < $cbn; $i++) {
    $checked = isset($cb[$i]) ? $cb[$i] : 0;
    // Do you insert over here
}

Open in new window


but when i unchecked any checkbox there data stored blank in database

Sir I to insert that checkbox value which is not checked
Image showing in below
chek.png
0
 
Julian HansenCommented:
What value must be inserted for a blank checkbox?
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
mohan singhWeb developerAuthor Commented:
value and name are same of checkbox
which is shown in image

if checkbox label is  VIEW then value is view,
if checkbox label is  UPDATE then value is update,
if checkbox label is  INACTIVE then value is inactive,  


Is there any other method without hidden field we  can stored unchecked checkbox value in Database
0
 
Julian HansenCommented:
No, the hidden value is the only way to pass it from the client to the server.

However, on the server you should know what those values are - I am assuming the server generated the page. So you could just have an array of values

$defaultValues = array(
  'view',
  'update',
  'inactive'
);

$cbn = isset($_POST['cbn']) ? $_POST['cbn'] : 0;
$cb = isset($_POST['obj_action']) ? $_POST['obj_action'] : array();
for($i = 0; $i < $cbn; $i++) {
    $default = isset($defaultValues[$i]) ? $default[$i] : -1;
    $checked = isset($cb[$i]) ? $cb[$i] : $default;
    // Do you insert over here
}

Open in new window

0
 
mohan singhWeb developerAuthor Commented:
Finally it done

Thank you Julian Hansen for help

I am insert that value like you said but using  some different  style


Thank you sir

Regards
Mohan SIngh
0
 
Julian HansenCommented:
You are welcome.

If your problem has been solved could you close the question as soon as it is convenient.
0
 
mohan singhWeb developerAuthor Commented:
thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.