how can i keep checked slecected value of checkboxs after submitting a form and keep them checked befor submit old values

  database, forms, html, mysql, php
<?php
    require_once('identifier.php');
    require_once('connexiondb.php');
    $idS=isset($_GET['idS'])?$_GET['idS']:0;
        

I did a command explode here but it didn’t work

?>
<!doctype html>
<html>
  <head>

  <?php
   
  if(isset($_POST['submit'])){

    if(!empty($_POST['Hist'])) {

      $Hist = implode(",",$_POST['Hist']);

      //  Update record
      $requetemisejour= "UPDATE Enseignant SET HistoriquePed='".$Hist."' ";
      $resultatdemiseajour=$pdo->query($requetemisejour);
        

I did a command explode here but it didn’t work inorder to show selected chekboxes

    }
    
  }
  ?>
  </head>
  <body>
  <form method="post" action="">
    <span>Select historique</span><br/>
    <?php
    
    $checked_arr = array();

    // Fetch checked values
    $requeteEnseig= "SELECT * FROM Enseignant where idenseignant=$idS";
    $resultatEnseig=$pdo->query($requeteEnseig);
    $fetchHist=$resultatEnseig->fetch();
    
    // Create checkboxes
    $Historique_arr = array("7ème","8ème","9ème","1ère");
    
    foreach($Historique_arr as $Historique){

      $checked = "";
     
      if(in_array($Historique,$checked_arr)){
        $checked = "checked";
        

i did this bloc : $_POST["Hist"]=explode (",",@$Hist);
echo $_POST["Hist"]; but it dont show the chechboxes selected before and after submit

      }
      echo '<input type="checkbox" name="Hist[]" value="'.$Historique.'" '.$checked.' > '.$Historique.' <br/>';
        
    }
    
    ?>
     <input type="submit" value="Submit" name="submit">

I did a command explode here but it didn’t show the checked chekboxes

  </body>
</html>

enter image description here

Source: Ask PHP

LEAVE A COMMENT