We help IT Professionals succeed at work.

php unchecked checkbox

BTMExpert
BTMExpert asked
on
Medium Priority
3,568 Views
Last Modified: 2012-05-12
How do you handle if a checkbox is not checked?  I still need to bring a value in when the form is submitted.

Ex.
3 checkboxes
2 checked
1 not

need to see something like this:
checked, checked, unchecked

is this possible
Comment
Watch Question

Top Expert 2011

Commented:
Forms submit checkboxes as comma delimited lists.

e.g., suppose you have this:

<input type="checkbox" name="vehicle" value="Bike" /> Bike<br />
<input type="checkbox" name="vehicle" value="Car" /> Car<br/>
<input type="checkbox" name="vehicle" value="Scooter" /> Scooter<br/>

Open in new window


If "Bike" and "Scooter" are checked, when the form is submitted, your checkbox will submit

vehicle=Bike,Scooter

Commented:
<input type="checkbox" name="vehicle" value="Bike"  checked/> Bike<br />
<input type="checkbox" name="vehicle" value="Car" checked /> Car<br/>
<input type="checkbox" name="vehicle" value="Scooter" /> Scooter<br/>

Commented:
It is best to give different names to the checkboxes:

<input type="checkbox" name="vehicleB" value="Bike"  checked/> Bike<br />
<input type="checkbox" name="vehicleC" value="Car" checked /> Car<br/>
<input type="checkbox" name="vehicleS" value="Scooter" /> Scooter<br/>


Then you can check each of them for a value.
Greg AlexanderLead Developer

Commented:
You will have to make a check for that something like

if(!isset($_POST['your_checkbox_name'])){
     $_POST['your_checkbox_name'] = '0';
}

Author

Commented:
now for the wrench :)
My inputs are array names

<input type="checkbox" name="vehicle[]" value="Bike"  checked/> Bike<br />
<input type="checkbox" name="vehicle[]" value="Car" checked /> Car<br/>
<input type="checkbox" name="vehicle[]" value="Scooter" /> Scooter<br/>

i need the array to read
vehicle[0] = 1;
vehicle[1] = 1;
vehicle[2] = 0;

Sorry
Lead Developer
Commented:
Are you storing these in a database? If so, you can remove them first and then reinsert them, so in that case you will not have to know what was unchecked, only what the new values are, make sense?
Unfortunately only as many arrays entries will be found, as are checked. Meaning if you have 2 checked and one unchecked you will only get two array entries. Which means if the first one is the unchecked one, the second check box will have an array index of 0 (meaning you can't tie a specific number to a specific check box). The value for each of the array entries will be the value you specify in the input value attribute: Meaning you will get:
vehicle[0] = Bike
vehicle[1] = Car

and that's all from your given example. HTH

Author

Commented:
I'm trying to update a database so if the box is checked, it sends a 1 to the database and 0 if not checked.  I can do it in asp.net but having trouble in php

Commented:
<input type="checkbox" name="vehicleB" value="Bike"  checked/> Bike<br />
<input type="checkbox" name="vehicleC" value="Car" checked /> Car<br/>
<input type="checkbox" name="vehicleS" value="Scooter" /> Scooter<br/>


then

if ( isset($_POST["vehicleB"])
  $bike=1;
else
  $bike=0;

and etc for the others.

Author

Commented:
i'm using an array so isset wouldn't work in this case
Greg AlexanderLead Developer

Commented:
Sounds like you may need to do a linking table instead, not sure what the checkboxes for, but for instance if it is a person clicking on types of vehicles they have the table structor would be

user_table
id   name  
1    Fred

vehicles_table
id    title
1     Bike
2     Car
3     Scooter

user_vehicles_linked
user_id     vehicle_id
1               2
1               3

Then when you update the user after they submit, you could delete all of them first
<?php
mysql_query("DELETE FROM user_vhicles_linked WHERE user_id = '".$user_id."");

foreach($_POST['vehicle'] as $key => $value){
      mysql_query("INSERT INTO user_vhicles_linked SET user_id = '".$user_id."' and vehicle_id = '".$v."'");
}

?>
to simply galexander's code and fix a typo:

Then when you update the user after they submit, you could delete all of them first
<?php
mysql_query("DELETE FROM user_vhicles_linked WHERE user_id = '".$user_id."");

foreach($_POST['vehicle'] as $v){
      mysql_query("INSERT INTO user_vhicles_linked SET user_id = '".$user_id."' and vehicle_id = '".$v."'");
}

?>

Open in new window

Author

Commented:
Almost there