jcstanley Posted February 1, 2007 Share Posted February 1, 2007 Hi As explained in an earlier post about storing checkbox values (1 or 0) into a databse - can the reverse be done? I am now wanting to retrive data from the database but can't seem to get the checkboxes that should have a value of 1 to be checked. $row = mysql_fetch_object($result); $yes=$row->yes; (value = 1) $no=$row->no; (value = 0) <input type="checkbox" name="yes" value="<?php print $yes; ?>"></td> <input type="checkbox" name="no" value="<?php print $no; ?>"></td> The "yes" checkbox should be ticked, but neither of them are. What am i doing wrong? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/36625-re-checking-checkboxes/ Share on other sites More sharing options...
ToonMariner Posted February 1, 2007 Share Posted February 1, 2007 If a check box is not selected then it is not even sent to the next page - ie. isset($_REQUEST['chkbx']) will be false. This behaviour is particular to checkboxes and radio buttons. All the other field types in html are sent even if their value is empty. The example you have above uses 2 boxes - you only need 1; they either check it or not so you can check in your code if that field has been set. Quote Link to comment https://forums.phpfreaks.com/topic/36625-re-checking-checkboxes/#findComment-174508 Share on other sites More sharing options...
hvle Posted February 1, 2007 Share Posted February 1, 2007 hi jcstanley, check box doesn't work like that. <input type="checkbox" name="yes" value="yes"> // is an uncheck checkbox <input type="checkbox" name="yes" value="yes" checked> // is a checked checkbox so you have to have a mechanism where if the value retrieved from db is 1, print "checked" inside the checkbox tag, otherwise, do not. Quote Link to comment https://forums.phpfreaks.com/topic/36625-re-checking-checkboxes/#findComment-174509 Share on other sites More sharing options...
ted_chou12 Posted February 1, 2007 Share Posted February 1, 2007 I think what you are looking for is this: <?php $row = mysql_fetch_object($result); if ($row->checkbox == 1) {echo "<input type=\"checkbox\" name=\"yes\" value=\"yes\" checked=\"yes\">";} else {echo "<input type=\"checkbox\" name=\"yes\" value=\"yes\">";} if ($row->checkbox == 0) {echo "<input type=\"checkbox\" name=\"no\" value=\"no\" checked=\"yes\">";} else {echo "<input type=\"checkbox\" name=\"no\" value=\"no\">";} Ted Quote Link to comment https://forums.phpfreaks.com/topic/36625-re-checking-checkboxes/#findComment-174511 Share on other sites More sharing options...
HuggieBear Posted February 1, 2007 Share Posted February 1, 2007 <input type="checkbox" name="yes" value="yes" checked> // is a checked checkbox You should use checked="checked" to make it standards compliant, not just checked JC, looking at the code, you're implying you have two columns, one called 'yes' and one called 'no'. Is that correct? Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/36625-re-checking-checkboxes/#findComment-174561 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.