samedi 1 décembre 2018

How to set checkboxes into 'checked' based from the values in database?

I want to make an "Edit" feature for my patients record. One of the requirement is to check checkboxes. I use this code to insert it in the database

<div class="col-xs-12 col-sm-6">
   <input type="checkbox" value="Sulfa drugs" name="Allergic[]">  
   <label>Sulfa drugs</label> 

   <input type="checkbox" value="Aspirin" name="Allergic[]">                 
   <label>Aspirin</label>  

   <input type="checkbox" value="Latex" name="Allergic[]">&nbsp;                 
   <label>Latex</label> 
</div>

<?php
   $Allergic = implode(', ', ($_GET['Allergic']));
      $sql = "INSERT INTO history_table (allergic) VALUES ('$Allergic')";
?>

This code is working successfully. The data were inserted if it's checked; as a string in one column only.

column allergic -> Aspirin, Latex

When the user clicked the edit button, the checkboxes that has been checked must be filled/checked.

I tried this code but it only works if it has one checked only, if its multiple, nothing happens

<input type="checkbox" value="Aspirin" name="Allergic[]" 
<?php echo ($row['allergic']=='Aspirin')?'checked':'' ?>>

<input type="checkbox" value="Latex" name="Allergic[]" 
<?php echo ($row['allergic']=='Latex')?'checked':'' ?>>

Hope someone can help me I've been working on this for almost a day. Thank you!




Aucun commentaire:

Enregistrer un commentaire