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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.