vendredi 25 octobre 2019

How to display checked check boxes together with non-checked ones

I have 5 check boxes from which a user can select one or more choices. The selected choices are then updated in database. The user's choices are then displayed/reviewed on another page. However my issue is that I want to show the updated choices together with the non-selected choices when doing a foreach loop in PHP.

These are the 5 check boxes

<input type="checkbox" name="interest[]" value="fishing">Fishing
<input type="checkbox" name="interest[]" value="camping">Camping
<input type="checkbox" name="interest[]" value="hiking">Hiking
<input type="checkbox" name="interest[]" value="swimming">Swimming
<input type="checkbox" name="interest[]" value="running">Running

<br><br>
<input type="submit" name="submit" value="Submit">

Heres the code that updates

if (isset($_POST["submit"])) {

 $interestArr = $_POST['interest'];


$interest = new Interest();
$newArr = implode(',', $interestArr);

$interest->updateInterests($id=19, $newArr);

}

Heres the code that displays

<?php 

 $interest = new Interest();
 $interests = $interest->showInterests($userid=19)->interests;

 $newArr = explode(',', $interests);

 foreach ($newArr as $data) {

 echo '<input type="checkbox" name="interest[]" value="'.$data .'" checked>'.$data;
}

The update choices are stored under the interests column in DB like so fishing,camping,running

And the foreach loop displays them checked check box with the correct corresponding labels.

How can I display the other check boxes that were not selected just so that the user might want to make changes?

Thanks.




Aucun commentaire:

Enregistrer un commentaire