Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default check a checkbox html

Tags:

html

php

I am trying to get a checkbox checked by default, but everything I have tried doesn't seem to work. I don't know if it has to do with the PHP that is in the code.

function show_subscription_checkbox ($id='0') {
    global $sg_subscribe;
    sg_subscribe_start();

    if ( $sg_subscribe->checkbox_shown ) return $id;
    if ( !$email = $sg_subscribe->current_viewer_subscription_status() ) :
        $checked_status = ( !empty($_COOKIE['subscribe_checkbox_'.COOKIEHASH]) && 'checked' == $_COOKIE['subscribe_checkbox_'.COOKIEHASH] ) ? true : false;
    ?>   
<p <?php if ($sg_subscribe->clear_both) echo 'style="clear: both;" '; ?>class="subscribe-to-comments">
        <input type="checkbox" name="subscribe" id="subscribe" value="subscribe" style="width: auto;" <?php if ( $checked_status ) echo 'checked="checked" '; ?>/>

        <label for="subscribe"><?php echo $sg_subscribe->not_subscribed_text; ?></label>
        </p>

This is a wordpress plugin that allows you to subscribe to blog comments.

I have tried

echo 'checked=\"checked\" ';
echo 'checked="checked" ' ;
echo 'checked> ';

The plugin author states that you used to be able to default check the checkbox but not anymore.

like image 878
myladeybugg Avatar asked Feb 07 '13 06:02

myladeybugg


People also ask

How do I set default check box value?

Use the defaultChecked prop to set the default checked value of a checkbox in React, e.g. defaultChecked={true} . Input elements with type set to checkbox support the defaultChecked prop for setting a default value.

What is the default value of checkbox?

But it also says (under the description of the input element) that the value attribute is required in this case. So <input type=checkbox name=foo> has undefined behavior as regards to the value used, though in practice browsers use value=on as the default.

What is the default property of checkbox?

The defaultChecked property returns the default value of the checked attribute. This property returns true if the checkbox is checked by default, otherwise it returns false.


2 Answers

Since this is showing up in google for "default checked checkbox", I figured I'd answer it. Alpay was right: The correct way to ensure that a checkbox is checked by default is like so (followed by an example of one that is not checked):

<input type="checkbox" name="vehicle" value="Car" checked> I have a car    
<input type="checkbox" name="vehicle" value="Bike"> I have a bike

Answer was found on w3schools. The author of the original question was having trouble with his PHP code, which is not at all related to the question title.

like image 180
EleventyOne Avatar answered Oct 06 '22 01:10

EleventyOne


In HTML, if you want a checkbox to be checked by default, see the following;

<input type="checkbox" name="name1" value="uc"> This checkbox is unchecked <br>
<input type="checkbox" name="name2" value="c" checked> This checkbox is checked<br>

So, you might consider changing

<?php if ( $checked_status ) echo 'checked="checked" '; ?>

to

<?php if ( $checked_status ) echo 'checked'; ?>
like image 33
Alpay Avatar answered Oct 06 '22 01:10

Alpay