Skip to content Skip to sidebar Skip to footer

Automatcially Check Checkbox Based On Database Array PHP

In the 'User Setting' tab of my page, I want the user to determine which types of posts from a specific user. Here is the form:
Copy

If you write "checked" at the end of the input Tag, this will be checked, as word says ;)

Another thing, don't mix tags:

<input type="checkbox" name="Name" value="Value" id="Id" checked>
<label>Life</label>

And finally, your code will work if you make this at the beginning:

$checked = 'checked';

But this will check all boxes, you will need to check something, like this:

<?php $checked = 'checked'; ?>
<input type="checkbox" name="categories[]" value="BusFin" id="BusFin" <?php echo if($something == $otherThing) echo $checked;?>   >

Solution 2:

<input type="checkbox" name="categories[]" value="Life" id="Life" <?php if($something) { echo "checked"; }?>>

this may vary depending of the array content
here some examples

$assoc_array = [
    "life" => "yes",
    "food" => "no",
];

if($assoc_array['life'] == "yes"){ echo "checked"; }

$array = [
    "life",
    "food"
];

if (in_array("food", $array)) {
    echo "checked";
}

$assoc_array_bool = [
    "life" => "whatever",
    "food" => "whatever"
];

if($assoc_array_bool['life']){ echo "checked"; }
// would not check the checkbox
// if you replaced $assoc_array_bool['life'] with $assoc_array_bool['sport']

Post a Comment for "Automatcially Check Checkbox Based On Database Array PHP"