Page 1 of 1

Setting Checkbox Values

Posted: Thu May 26, 2005 3:58 am
by paolo
Hello,
I'm having some troubles setting the values of checkboxes into a form,
I've all the data saved into mysql DB with values set to 1 if checkbox should be checked.I tought the following code should have worked but doesn't seems to me

Code: Select all

if($valv_tbl_ad == 1) $valv_tbl_ad = "on"; else $valv_tbl_ad = "off";
...

Code: Select all

<td width=\"4%\" align=\"center\"><input type=\"checkbox\" name=\"valv_tbl_ad\" value=\"$valv_tbl_ad\"></td>
any suggestion?
Thanks in advance
Paolo

Posted: Thu May 26, 2005 4:12 am
by Ruski
Its not "on" or "off" its "1" or "0"

Posted: Thu May 26, 2005 4:21 am
by paolo
I've just tryied keeping them to 1, during insert fase in DB it doesn't save me the values, since it's "on" and I had to convert to 1 with

Code: Select all

if($valv_tbl_ad == "on") $valv_tbl_ad = 1; else $valv_tbl_ad = 0;
now even if I do

Code: Select all

<td width=\"5%\" align=\"center\"><input type=\"checkbox\" name=\"valv_tbl_ad\" value=\"1\"></td>
it doesn't check it!

thanks again

Posted: Thu May 26, 2005 4:30 am
by Ruski
Look at how punbb does it:

Code: Select all

<label><input type=&quote;checkbox&quote; name=&quote;form&#1111;use_avatar]&quote; value=&quote;1&quote;<?php if ($user&#1111;'use_avatar'] == '1') echo ' checked=&quote;checked&quote;' ?> /><?php echo $lang_profile&#1111;'Use avatar'] ?><br /></label>

Posted: Thu May 26, 2005 4:56 am
by shiznatix

Code: Select all

<input type="checkbox" value="whatever" name="whatever" <? echo ($user['use_avatar'] == '1' ? 'CHECKED' : ''); ?>>

Posted: Thu May 26, 2005 8:57 am
by paolo
I've still got a problem, all my form is in echo"...."; how do I tell to perform the if statement? I tryied putting with and without <? ?> but doens't work
Thanks

Posted: Thu May 26, 2005 9:03 am
by neophyte
Don't echo the whole form just the parts that you need to echo.

For example you can do stuff like this...

Code: Select all

//somthing like
<?php if (some condition){ ?>
//HTML GOES HERE WITH NO ECHO

<?php } ?>

Posted: Thu May 26, 2005 9:24 am
by paolo
I did so to fill the form in auto, suppose I take a lot of paramenters from $_POST, the form I'm working on is 74 field, a lot are checkbutton, how do I set parameters in auto? now I do value=\"$parameter\"; and I do also this because of autenthication, if user is not logged he doens't sees anything.
Paolo

Posted: Fri May 27, 2005 12:05 am
by harrisonad
the 'value=' property of a checkbox doesn't make it checked. But rather include a CHECKED option inside it.

Code: Select all

echo "<input type=checkbox name=ThisCheckBox value=ThisValue CHECKED>";
So with regards to your problem, include an if statement inside the input.

Code: Select all

echo "<input type=checkbox name=ThisCheckBox value=ThisValue ".($isChecked?"CHECKED":"").">";
If your problem persists, lemme know. ok?

Posted: Fri May 27, 2005 12:39 am
by vigge89
checked='checked' for XHTML compability :wink: