编写动态复选框列表,如何提交复选框?

时间:2018-11-19 20:35:00

标签: php html mysql

我正在列出学生名单,以便用户可以标记他们的缺席。我想拥有一个表格,其中包含用户名册上每个学生的复选框及其名称。我有生成该代码。点击提交后,如何编写一个使用所有复选框将带有每个学生ID的日期添加到数据库的查询?

这是我的代码:

//set up the database 
$servername = "localhost";
$usernameDB = "root";
$passwordDB = "";
$nameDB = "teacheasy";
//connect to the database
$connection = new mysqli($servername, $usernameDB, $passwordDB, $nameDB);


$sqlListStudents = "SELECT CONCAT(last_name,', ',first_name) AS 'StudentName' FROM `students` WHERE teacher_id='" . $_SESSION['userId'] . "' ORDER BY last_name ASC;";
$resultListStudents = mysqli_query($connection, $sqlListStudents) or die("Bad Query: $sqlListStudents"); 
echo "<form method='post'>";
echo"<table>";
echo"<tr>
        <td><b>Mark Today's Absesences Only</b></td>
     </tr>";
$nameCount = 0;
while($row = mysqli_fetch_assoc($resultListStudents)){
    echo"<tr><td><input type='checkbox' name='student". $nameCount ."'>{$row['StudentName']}</td></tr>";
    $nameCount++;
}
echo "</table>";
echo "<button type='submit' name='submitAbsences' class='navButton'>Submit Absences</button>";
echo "</form>";

if(isset($_POST['submitAbsences'])){

}

这是我的输出。 Click this, I cant put pictures yet due to being a newer account.

1 个答案:

答案 0 :(得分:-1)

如果将复选框的名称设置为name="students[]",PHP可以使用list function将所有名称存储在数组中。将其放入数组后,您可以更新遍历该数组的循环。