Reputation: 5
I am trying to create a student attendance table that contains a checkbox for each students attendance, that is to be stored in a database.
This is my table that I have so far, that is executed from an ajax function call. Most of the data is from a database, that contains student names.
For the sake of this example $numStudent = 5;
echo '<form method="post" action="checkAttend.php">
<table border="1">';
$count = 1;
while(($count < $numStudent) && ($students = mysqli_fetch_assoc($result))) {
echo '<tr>
<th>' . $count. '</th>
<th>' . $students['student_name'] . '</th>
<th> <input type="checkbox" name="students[]" /> </th>
</tr>';
$count++;
}
echo '<th colspan = "3">
<input type="submit" name="button" id="button" value="Submit" /> </th>';
echo '</table>';
echo '</form>';
In checkAttend.php
The form prints exactly the way I need it to, but when I try to print out the values in the checkbox array (to check what values are contained within before saving it to the database) each value in the array prints out :
$student_attend[0] A
$student_attend[1] r
$student_attend[2] r
$student_attend[3] a
$student_attend[4] y
Typically I want to store some kind of value in the corresponding field in the database to indicate if the student is absent or not.
I cant tell if I am doing the checkbox loop correctly or not or if I need to add in more stuff.
Upvotes: 0
Views: 513
Reputation: 4033
If you want to add dynamic data with your checkboxes, then you can use it as follows: Inside the form tag you can use
<form method="post" action="checkAttend.php">
<table border="1">
<?php
$count=1;
while($students = mysqli_fetch_assoc($result)) {
?>
<tr>
<th><?php echo $count;?></th>
<th><?php echo $student['student_name']; ?></th>
<th><input type="checkbox" name="students[]" value=<?php echo $student['student_id']; ?>/></th>
</tr>
<?php
}
?>
</table>
</form>
and in checkAttend.php page write the following code:
if(isset($_POST['button'])){
if(count($_POST['student_id'])>0){
foreach($_POST['student_id'] as $student_id){
mysql_query("update table_name set column_name = '1' where student_id = '".$student_id.'");
// Just use the student id as checkbox value and update it as you just need the check or uncheck the checkbox.
}
}
}
Upvotes: 0