Setting Checkbox Values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paolo
Forum Newbie
Posts: 15
Joined: Sat May 14, 2005 6:06 am

Setting Checkbox Values

Post 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
Ruski
Forum Commoner
Posts: 28
Joined: Thu May 26, 2005 3:45 am

Post by Ruski »

Its not "on" or "off" its "1" or "0"
paolo
Forum Newbie
Posts: 15
Joined: Sat May 14, 2005 6:06 am

Post 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
Ruski
Forum Commoner
Posts: 28
Joined: Thu May 26, 2005 3:45 am

Post 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>
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Code: Select all

<input type="checkbox" value="whatever" name="whatever" <? echo ($user['use_avatar'] == '1' ? 'CHECKED' : ''); ?>>
paolo
Forum Newbie
Posts: 15
Joined: Sat May 14, 2005 6:06 am

Post 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
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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 } ?>
paolo
Forum Newbie
Posts: 15
Joined: Sat May 14, 2005 6:06 am

Post 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
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post 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?
Last edited by harrisonad on Fri May 27, 2005 12:44 am, edited 1 time in total.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

checked='checked' for XHTML compability :wink:
Post Reply