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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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