mardi 11 mai 2021

Insert dropdown values with checkbox in php

here is the dropdown image

Cannot insert selected dropdown values with checkbox.It's only inserting the first value of the dropdown in all row.When I click checkbox and click enroll button ,select option doesn't inserting selected value.I have 3 options(Regular,Retake,Recourse) in "examtype" table.So how to insert select option value in database using checkbox?When I click checkbox and click enroll button selected values from dropdown is not inserting.Below is my code.


 <?php
if (isset($_POST['enroll']))
{
if (!empty($_POST['chk1'])) 
{
  if (!empty($_POST['et']))
  {
    $roll_no = $_SESSION['roll_no'];
    $selectbox1 = $_POST['et'];
foreach($_POST['chk1'] as $checkbox1)
{
foreach($_POST['et'] as $selectbox1)
$values1 = explode("|", $selectbox1);
$values = explode("|" , $checkbox1);
$course_id= $values[0];
$semester= $values[1];
$course_name= $values[2];
$selectbox1=$values[3];

$sql="INSERT INTO pendingcourse(roll_no,course_id,semester,course_name,exam_type,status) 
VALUES('$roll_no','$course_id','$semester','$course_name','$selectbox1',0)";
$stmt = $connect->prepare($sql);
$stmt->execute();
$checkbox1='';
$selectbox1='';
}
header("location:pending.php");
}
}
}
?>



<form  action="praticadel.php" method="post" enctype="multipart/form-data"> 
<div class="container mt-3" >
    <div class="accordion mb-4" id="accordionExample" >
        <div class="accordion-item" style="background-color: rgb(0, 225, 255);">
          <h2 class="accordion-header" id="headingOne">
            <button class="accordion-button collapsed " type="button" data-bs-toggle="collapse" 
data-bs-target="#collapseOne" aria-expanded="false" aria-controls="collapseOne" 
style="background: #AAFFA9;">
              1st Semester
            </button>
          </h2>
                <table class="table table-bordered border-primary text-center">
                <p> <input type="checkbox" id="selectAll"/>Select All</p>
                    <thead>
                      <tr>
                        <th scope="col-1">Course Id</th>
                        <th scope="col-1">Semester</th>
                        <th scope="col-7">Course</th>
                        <th scope="col-1">Exam Type</th>
                        <th scope="col-1">Select</th>
                         
                      </tr>
                    </thead>
                    <tbody>  
                        

 <?php
 $stmt = $connect->query("SELECT course_id,semester,course_name,seat_available 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="et[]" id="selectBox" class="form-select form-select-sm" 
    aria-label=".form-select-sm example" onchange="OnSelectionChange">
                        
                        <?php 
                              $stmt1 = $connect->query("SELECT exam_type from examtype");
                              $rows1 = $stmt1->fetchAll(PDO::FETCH_ASSOC);
                              foreach($rows1 as $row1) {
                         ?>
                              <option value="<?php echo $row1['exam_type']; ?>" 
   selected="selected"><?php echo $row1['exam_type']; ?></option>
                         <?php 
                            }
                          ?>
                        </select>
                        </td> 
                        <td>  
                        <input type="checkbox" class="ch" id="checked" onclick="check();" 
   name="chk1[] " value="<?php echo $row['course_id']?>|<?php echo $row['semester']?>|<?php echo 
   $row['course_name']?>|<?php echo $row1['exam_type']?>">
                            <label class="form-check-label" for="flexCheckDefault">
                            </label>  
                        </td>
                      </tr>                     
     <?php
      }
     ?>
                    </tbody>  
              </table>
          </div>
        </div>
      </div>
    </div>
    <div class="container">
  <div class="row">
    <div class="col-md-2 col-xs-6 offset-md-5 mb-3">

    <button type="submit"  name="enroll" class="btn btn-warning mt-2" id="saveChanges"  
    >Enroll</button>
    </div>
  </div>
   </div>
   </form>



Aucun commentaire:

Enregistrer un commentaire