Jump to content

checkbox echo checked based on database


laflair13

Recommended Posts

Sorry guys but I am back with another question. 

 

I have tried quite a few different ways and I cannot seem to get this to work. I have checkboxes being stored as 1=checked, 0=not check but when I go to the item page the checkbox isnt checked.

 

Here are some codes that I have tried

<input type="checkbox" name="featured" <?php if ($featured == '1') echo "checked='checked'"; ?> class="form-control" />

<input type="checkbox" name="featured" <?php if($var) { echo 'value="checked"'; }?> class="form-control" />

<input type="checkbox" name="featured" value="1" <?php echo ($row1['featured'] == 1) ? 'checked="checked"' : ''; ?> class="form-control" /> (This shows it checked even when its not)

Any help would be appreciated.

 

 

Link to comment
https://forums.phpfreaks.com/topic/295138-checkbox-echo-checked-based-on-database/
Share on other sites

Well I was able to get that working but now if I edit the item and uncheck the box, it does not change the value in the database from 1 to 0. 

 

input for 1 of the checkboxes. I have 10 of them

 <input type="checkbox" name="showmodel" <?php if ($showmodel == '1') echo "checked='checked'"; ?> />

Here is my update query

$query = "UPDATE `new_equip` SET `featured`='1',`showmanu`='1',`showmodel`='1' "WHERE `id`='$id' LIMIT 1";

To build on what Barand is saying, checkboxes that are not checked do not get sent in a GET/POST request. Only checked checkboxes (and radio selectors). So you need to check to see if it exists in POST/GET, if it does not, set a value like 0 before saving in the database. See Barands link above for an example.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.