samedi 27 mars 2021

How to insert selected value from select option using checkbox in PHP?

how to insert select option value in database using checkbox?When I click checkbox and click enroll ,select option doesn't inserting selected value when i choose more then two.I have 3 options(Regular,Retake,Recourse) in "examtype" table but when I choose more then one subject select option not working perfectly. Here is my code... here is my select option image

 <?php
if (isset($_POST['enroll'])){
if (!empty($_POST['chk1'])) {
foreach($_POST['chk1'] as $checkbox1){
  if (isset($_POST['exam_type'])){
foreach($_POST['exam_type'] as $checkbox2 )

$values = explode("|" , $checkbox1);
$values1 = explode("|" , $checkbox2);
$course_id= $values[0];
$semester= $values[1];
$course_name= $values[2];
$exam_type=$values1[0];

$sql="INSERT INTO pendingcourse(course_id,semester,course_name,exam_type) VALUES('$course_id','$semester','$course_name','$exam_type')";
$stmt = $connect->prepare($sql);
$stmt->execute();
$checkbox1='';
$checkbox2='';

}
}
header("location:coursetable.php");
}
}
?>

<?php
$stmt = $connect->query("SELECT course_id,semester,course_name FROM coursetable Where semester = '1st' ");
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row)
{
   $course_id = $row['course_id'];
  $semester = $row['semester'];
   $course_name = $row['course_name'];

?>
                    <tr>
                        <td scope="row"> <?php echo $course_id?></td>
                        <td > <?php echo $semester ?></td>
                        <td ><?php echo $course_name ?></td>
                        <td>
                        
                        <select name="exam_type[]" class="form-select form-select-sm" aria-label=".form-select-sm example">
                            <?php 
                              $stmt1 = $connect->query("SELECT * from examtype");
                              $rows1 = $stmt1->fetchAll(PDO::FETCH_ASSOC);
                              foreach($rows1 as $row1) { ?>
                                    <option><?php echo $row1['exam_type']; ?></option>
                                    <?php 
                                    }
                            ?>
                        </select>
                    
                     
                        </td>  
                        <td>     
                        <input type="checkbox" name="chk1[]" value="<?php echo $row['course_id']?>|<?php echo $row['semester']?>|<?php echo $row['course_name']?>">
                            <label class="form-check-label" for="flexCheckDefault">
                            </label>  
                        </td>
                        <td></td>
                      </tr>                     
 <?php
}
?>
                    </tbody> 
              </table>
          </div>
        </div>
      </div>
    </div>
</div>
<div class="container">
  <div class="row">
    <div class="col-md-2 col-xs-6 offset-md-5">
    
    <button type="submit" name="enroll" class="btn btn-warning mt-2">Enroll</button>



Aucun commentaire:

Enregistrer un commentaire