在复选框php中存储两个值

时间:2018-03-06 15:45:44

标签: php

我正在尝试记录每个学生的出勤率,我能够检索复选框的值(Late,Ocuse,Absent)。有没有办法可以从复选框中获取学生证和出勤标记?我需要将学生证与他/她的出勤标记配对。

<!-- I want to get the student ID, to save his/her Mark(Late,Absent,Excused) -->


<form action="WorkLoad_attendance.php" method="post">					 
<div id="employee_table" class="" >  
<table class="table table-bordered">
<?php 
 $connect = mysqli_connect("localhost", "root", "", "smis");  
 $query = "SELECT DISTINCT  ST.*,SL.*,W.*,C.className,SJ.* FROM studentdata ST,workloaddata W,subject_studentlist_data Sl,classdata C,subjectdata SJ 
 WHERE (((((SL.studentList_studentID = ST.studentID)and(SL.studentList_WorkloadID = W.WorkloadID))and(W.Workload_classID= C.classID)) and SL.studentList_subjectID =SJ.subjectID) and SL.Enrollment_Status ='Enrolled') and W.WorkloadID=$WID";   
 $result = mysqli_query($connect, $query); 
?>
 <tr>  
 <th width="27%">Student Name</th>                                     
 <th width="25%">Class Name</th>
 <th width="23%">Subject Name</th> 									
 <th width="9%">Present But Late</th>
 <th width="9%">Excuse</th>
 <th width="7%">Absent</th>										
 </tr>  
     <?php  
     while($row = mysqli_fetch_array($result))  
     {   						   	
     ?>  

 <tr>  
 <td><?php echo  "&nbsp;".$row["Lname"].", &nbsp;".$row["Fname"] ?></td> 
 <td><?php echo  "&nbsp;".$row["className"] ?></td>
 <td><?php echo  "&nbsp;".$row["subjectName"] ?></td>
 <td><input type="checkbox" name="mark[]" value=""><label>&nbsp; Late</label></td>
 <td><input type="checkbox" name="mark[]" value="Excused"><label>&nbsp;Excused</label></td>
 <td><input type="checkbox" name="mark[]" value="Absent"><label>&nbsp;Absent</label></td>                </tr> 
	<input type="hidden" name="A_StudentlistID" value="<?php echo $row["studentList_studentID"];?>					
<?php   					   
 }                              							  
mysqli_close($connect);							   
 ?>  
 </table>
 
<input type="button" name="ResetSelected" value="Reset Selected" class="btnCheckboxes2 " />
<input type="submit" name="RecordAttendance" value="Record Attendance" class="btnCheckboxes " />						
         </div> 
      </form >					 




<?php

if(isset($_POST['RecordAttendance']))
 {  //$StudentID = $_POST['markAttendance'];
    $A_StudentlistID = $_POST['A_StudentlistID'];
	$SL_subjectID = $_POST['SL_subjectID'];
echo "student list:  $A_StudentlistID";

 if( isset($_POST['mark']) && is_array($_POST['mark']) ) {
    foreach($_POST['mark'] as $mark) {
        // Displaying mark for testing"
        echo "<br>student list:  $A_StudentlistID Attendance Mark {$mark}!<br>";
		
        // -- insert into database call will go here
    }
 }

 }
?>

3 个答案:

答案 0 :(得分:0)

将学生ID放在名称中。 PHP的非标准表单处理例程会将它扩展为关联数组中的键:

<input type="checkbox" name="mark[<?php echo htmlspecialchars($row['id']) ?>]" value="Late">

然后当你处理它们时,你可以循环它们:

foreach ($_POST['mark'] as $student_id => $attendance) {
    echo htmlspecialchars($student_id) . " was " . htmlspecialchars($attendance) . ".";
}

答案 1 :(得分:0)

如果您想在多个值中使用相同的student_id:

    <input type="checkbox" name="mark[0_1]" value="Late"><label>&nbsp; Late</label>
    <input type="checkbox" name="mark[1_1]" value="Excused"><label>&nbsp; Excused</label>
    <input type="checkbox" name="mark[2_2]" value="Absent"><label>&nbsp; Absent</label>

foreach($_POST['mark'] as $key => $value) {
  $exp = explode('_', $key);
  $student_id = (int)$exp[1];
}

答案 2 :(得分:-1)

确定。使用隐藏字段。

<input type="hidden" name="student_id" value="<?php echo $theStudentId;?>" />

然后按名称

抓住它
$student_id = $_POST['student_id'];