Jump to content

echoing check box


droidus

Recommended Posts

<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.

Link to comment
https://forums.phpfreaks.com/topic/248064-echoing-check-box/#findComment-1273722
Share on other sites

@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.

Link to comment
https://forums.phpfreaks.com/topic/248064-echoing-check-box/#findComment-1273744
Share on other sites

@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 ""; 

Link to comment
https://forums.phpfreaks.com/topic/248064-echoing-check-box/#findComment-1273763
Share on other sites

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" : "")." />";
Link to comment
https://forums.phpfreaks.com/topic/248064-echoing-check-box/#findComment-1273793
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.