TecTao Posted October 8, 2014 Share Posted October 8, 2014 I'm trying to get some code to work that will update a field to show a checkbox either checked or not checked. The field either uses a Y or N. Y is checked, N not checked. A code that I have tried displays the correct result for the box either checked or not checked. input type="checkbox" name="obcDisplay" value="Y" <?=($r['obcDisplay'] == 'Y') ? 'checked="checked"' : ''; ?>/> But I want the ability to change when the form is submitted. so if I bring up a page and the checkbox is checked (Y) and I submint, I want the database to update to N and the other way around. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 8, 2014 Share Posted October 8, 2014 (edited) So basically, you want the checkbox to reflect whatever is in the database? If so, you just need to read in the checkbox value from the database prior to displaying the form. Then assign whatever the database contains to $r['obcDisplay'] and display the form. If a value isn't found in the database, you can set $r['obcDisplay'] to whatever you want the default to be. Edited October 8, 2014 by cyberRobot Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 8, 2014 Share Posted October 8, 2014 As a side note: Instead of using a string value of 'Y' or 'N' that you then need to test, it is more efficient to use 1 or 0 since those can logically be interpreted as the Boolean TRUE/FALSE values. <?=($r['obcDisplay']) ? 'checked="checked"' : ''; ?> 1 Quote Link to comment Share on other sites More sharing options...
TecTao Posted October 9, 2014 Author Share Posted October 9, 2014 Using the 1 or 0 as the value would make more sense. What I am having trouble understanding and making work would be, if 1 = checked and 0 = not checked. And the field for a particular user in the user table is set to 0 which returns an unchecked checkbox, I want to be able to check the box, click submit and update the DB field obcDisplay to a 1, and back and forth. <input type="checkbox" name="obcDisplay" value="<?=$r['obcDisplay'] ?>" <?=($r['obcDisplay']) ? 'checked="checked"' : ''; ?>/> so I put the result from the query to display the content of the obcDisplay field in the value = and it does display the correnct 1 or 0, and if it's a 1 the box is checked, if it's a 0 the box is unchecked. It's updating the table with the new 1 or 0 that I can't get my arms around. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 9, 2014 Share Posted October 9, 2014 It's updating the table with the new 1 or 0 that I can't get my arms around. I think the problem may be that you expect that the value in the POST data will tell you if it is checked or not. When you are dealing with a checkbox with a unique name, the value is typically irrelevant. The reason is that only CHECKED checkboxes are returned in the POST data. So, simply checking if the POST value for that field isset() is enough to tell you if it was checked or not. $obcDisplay_PostValue = isset($_POST['obcDisplay']) ? 1 : 0; //Insert this value into the DB. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.