2

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:

<form method="post" action="" name="permitted_categories">
                Select or deselect which types of Posts that you would like to see. 
                <p>
                <?
                $cat_query = mysqli_query($con, "SELECT permitcat FROM permitted WHERE username='$userLoggedIn' AND friendname='$username'")
                ?>
                    <label>
                      <input type="checkbox" name="categories[]" value="Life" id="Life" <? echo $checked;?>>
                      Life</label>
                    <br>
                    <label>
                      <input type="checkbox" name="categories[]" value="Politics" id="Politics" <? echo $checked;?>>
                      Politics</label>
                    <br>
                    <label>
                      <input type="checkbox" name="categories[]" value="Entertainment" id="Entertainment" <? echo $checked;?>>
                      Entertainment</label>
                    <br>
                    <label>
                      <input type="checkbox" name="categories[]" value="Food" id="Food" <? echo $checked;?>>
                      Food</label>
                    <br>
                    <label>
                      <input type="checkbox" name="categories[]" value="BusFin" id="BusFin" <? echo $checked;?>>
                      Business/Financial</label>
                    <br>
                    <label>
                      <input type="checkbox" name="categories[]" value="Fitness" id="Fitness" <? echo $checked;?>>
                      Fitness/Health</label>
                    <br>
                    <input type="submit" name="update_categories" id="update_categories">
                    <? 
                      if(isset($_POST['update_categories'])) {
                          $permitted_categories = $_POST['categories'];

                          echo "You will only see the following categories from ". $profile_user_obj->getFirstAndLastName() .": ";
                          foreach ($permitted_categories as $permcat){
                            echo $permcat .", ";
                        }

                          $values  = implode(", ", $permitted_categories);
                          $permit_query = mysqli_query($con, "INSERT INTO permitted (id, username, friendname, permitcat) VALUES('','$userLoggedIn', '$username', '$values')");   
                        } ?>
              </p>
            </form>

What I am trying to do is automatically check the boxes where the corresponding values are found in an array.

I have tried several things, but cannot get the code to work.

2
  • What is responding this query. $cat_query = mysqli_query($con, "SELECT permitcat FROM permitted WHERE username='$userLoggedIn' AND friendname='$username'"). Depende on the response you have to compare response array with your field then either 'checked' or not. Commented Mar 26, 2017 at 20:56
  • I had initially created an IF statement that compared the checkbox array to the array in the database and then sent an UPDATE, however, that didn't work and instead created dual entries into the database where on was correct and the other didn't insert anything in to permitcat column. Commented Mar 26, 2017 at 22:26

2 Answers 2

1

this is pretty simple.

The HTML documentation:

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

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;?>   >
Sign up to request clarification or add additional context in comments.

1 Comment

This didn't help with my original question.
1
<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']

3 Comments

Regarding checking for "yes", you can use PHP's filter_var to check common checked values: filter_var($assoc_array['life'], FILTER_VALIDATE_BOOLEAN)`.
I used the if(in_array("food", $array)){ echo "checked";} However my $array, is pulling from the database. I echo out the array information at the top of the page and it is getting the information from the database, but it doesn't seem that in_array is working as it should.
those were examples, and since your database might not have the same structure as the arrays i've shown you : this has little to no chance to work. if you posted the output of the sql request we could get you a working example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.