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
paolo
Forum Newbie
Posts: 15 Joined: Sat May 14, 2005 6:06 am
Post
by paolo » Thu May 26, 2005 3:58 am
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 » Thu May 26, 2005 4:12 am
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 » Thu May 26, 2005 4:21 am
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 » Thu May 26, 2005 4:30 am
Look at how punbb does it:
Code: Select all
<label><input type="e;checkbox"e; name="e;formїuse_avatar]"e; value="e;1"e;<?php if ($userї'use_avatar'] == '1') echo ' checked="e;checked"e;' ?> /><?php echo $lang_profileї'Use avatar'] ?><br /></label>
shiznatix
DevNet Master
Posts: 2745 Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:
Post
by shiznatix » Thu May 26, 2005 4:56 am
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 » Thu May 26, 2005 8:57 am
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
neophyte
DevNet Resident
Posts: 1537 Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota
Post
by neophyte » Thu May 26, 2005 9:03 am
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 » Thu May 26, 2005 9:24 am
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
harrisonad
Forum Contributor
Posts: 288 Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:
Post
by harrisonad » Fri May 27, 2005 12:05 am
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.
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Fri May 27, 2005 12:39 am
checked='checked' for XHTML compability