PHP checkbox set to check based on database value

前端 未结 5 1982
春和景丽
春和景丽 2020-12-07 23:41

I have a system where people fill in their information and later can go back and edit certain parts, basically the enter personal info and check whether they want to know ex

相关标签:
5条回答
  • 2020-12-08 00:00

    Extract the information from the database for the checkbox fields. Next change the above example line to: (this code assumes that you've retrieved the information for the user into an associative array called dbvalue and the DB field names match those on the HTML form)

    <input type="checkbox" name="tag_1" id="tag_1" value="yes" <?php echo ($dbvalue['tag_1']==1 ? 'checked' : '');?>>
    

    If you're looking for the code to do everything for you, you've come to the wrong place.

    0 讨论(0)
  • 2020-12-08 00:00

    This simplest ways is to add the "checked attribute.

    <label for="tag_1">Tag 1</label>
    <input type="checkbox" name="tag_1" id="tag_1" value="yes" 
        <?php if($tag_1_saved_value === 'yes') echo 'checked="checked"';?> />
    
    0 讨论(0)
  • 2020-12-08 00:05

    You can read database value in to a variable and then set the variable as follows

    $app_container->assign('checked_flag', $db_data=='0'  ? '' : 'checked');
    

    And in html you can just use the checked_flag variable as follows

    <input type="checkbox" id="chk_test" name="chk_test" value="1" {checked_flag}>
    
    0 讨论(0)
  • 2020-12-08 00:08

    Use checked="checked" attribute if you want your checkbox to be checked.

    0 讨论(0)
  • 2020-12-08 00:22

    Add this code inside your input tag

    <?php if ($tag_1 == 'yes') echo "checked='checked'"; ?>
    
    0 讨论(0)
提交回复
热议问题