droidus Posted September 28, 2011 Share Posted September 28, 2011 how do i echo "checked=checked" for a checkbox? here is what i have: <input type='checkbox' name='ip_automatic_login' id='ip_automatic_login' value='1' if($IPCheck == true) {/"checked=checked/" } /> this code will be echoed! Quote Link to comment Share on other sites More sharing options...
codefossa Posted September 28, 2011 Share Posted September 28, 2011 <input type="checkbox" name="ip_automatic_login" id="ip_automatic_login" value="1" <?php if ($IPCheck) { ?> checked="1" <?php } ?> /> or $checked = $IPCheck ? 1 : 0; echo '<input type="checkbox" name="ip_automatic_login" id="ip_automatic_login" value="1" checked="' . $checked . '" />'; Either should work fine. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 28, 2011 Share Posted September 28, 2011 @Kira, The proper way to check a checkbox is with checked="checked" $checked = ($IPCheck) ? "checked='checked' "; : ''; echo "<input type='checkbox' name='ip_automatic_login' id='ip_automatic_login' value='1' {$checked}/>"; Quote Link to comment Share on other sites More sharing options...
xyph Posted September 28, 2011 Share Posted September 28, 2011 @Kira, The proper way to check a checkbox is with checked="checked" $checked = ($IPCheck) ? "checked='checked' "; : ''; echo "<input type='checkbox' name='ip_automatic_login' id='ip_automatic_login' value='1' {$checked}/>"; checked When the type attribute has the value "radio" or "checkbox", this boolean attribute specifies that the button is on. User agents must ignore this attribute for other control types. http://www.w3.org/TR/html4/interact/forms.html#adef-checked Funny, how it says 'that that button is on' and not 'if the button is on' though it's a boolean attribute. Leads to a little confusion. But you are correct. Quote Link to comment Share on other sites More sharing options...
droidus Posted September 28, 2011 Author Share Posted September 28, 2011 @Kira, The proper way to check a checkbox is with checked="checked" $checked = ($IPCheck) ? "checked='checked' "; : ''; echo "<input type='checkbox' name='ip_automatic_login' id='ip_automatic_login' value='1' {$checked}/>"; i am using an echo statement though, so i had to change the code around: echo " $checked = ($IPCheck) ? \"checked='checked' \"; : ''; echo \"<input type='checkbox' name='ip_automatic_login' id='ip_automatic_login' value='1' {$checked}/>\"; "; all i get though, is this: ? = (1) ? "checked='checked' "; : ''; echo ""; Quote Link to comment Share on other sites More sharing options...
Buddski Posted September 29, 2011 Share Posted September 29, 2011 You are echoing the ternary operator, that is why you are getting that output.. If you are already in an echo statement when trying to add the checked you can do this, but what mjdamato said was correct, if left unmodified. echo "<input type='checkbox' name='ip_automatic_login' id='ip_automatic_login' value='1' ".($IPCheck == true ? "checked=checked" : "")." />"; Quote Link to comment Share on other sites More sharing options...
droidus Posted September 29, 2011 Author Share Posted September 29, 2011 oh, ok, it does work. sorry Quote Link to comment 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.