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
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";
Edited by laflair13
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.