Jump to content

re-checking checkboxes


jcstanley

Recommended Posts

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<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

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.