简体   繁体   中英

default value of checkbox in php

I have a checkbox with different symptoms: http://dentopolis.org/test/ I'd like some checkboxes to be checked as default (like "no toothpain") and then the user can change it's value once again. how can I do that? my current code:

$hiv=$_SESSION['hiv'] = $_GET['hiv'];

is it correct if I want the checkbox to be checked at the start?

$isHivChecked = ""; // Initiate new variable
if (isset($_GET['hiv'])=='on') {
    $isHivChecked = "checked";
    echo "<span class='czerwony'> HIV </span> <input type='checkbox' name='hiv' ".$isHivChecked.">";
}
else {
    $isHivChecked = "checked";
    echo "HIV <input type='checkbox' name='hiv' ".$isHivChecked.">";
}

UPDATED

It took me while to understand your needs. So please check this code.

if (empty($_GET)) {
    $isHivChecked = "checked";
} else {
    if (!empty($_GET['hiv'])) {
        $isHivChecked = "checked";
    } else {
        $isHivChecked = "";
    }
}

All you need to do just add an hidden input field to your form attribute. Then It'll work.

eg:

<input type="hidden" name="hdnField" value="0">

You can do it by (Old Answer):

$hiv=$_SESSION['hiv'] = $_GET['hiv'] = 'on';
$isHivChecked = ""; // Initiate new variable

if ($_GET['hiv']=='on') {
    $isHivChecked = "checked"; // If checked then value of $isHivChecked will be checked.
}

/* Finally put $isHivChecked into input attribute by concat system. */
echo "<span class='czerwony'> HIV </span> <input type='checkbox' name='hiv' ".$isHivChecked.">"; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM