I am creating a form that involves checkboxes. There is 5 checkboxes in a row. If the user adds another row, there will be another 5 checkboxes.
The code i used,
<tbody id="dataTable">
<tr>
<td>
<input type="text" class="form-control" name="startTime[]">
</td>
<td>
<input type="text" class="form-control" name="endTime[]">
</td>
<td>
<input type="checkbox" name="Monday[0]" value="1">
</td>
<td>
<input type="checkbox" name="Tuesday[0]" value="1">
</td>
<td>
<input type="checkbox" name="Wednesday[0]" value="1">
</td>
<td>
<input type="checkbox" name="Thursday[0]" value="1">
</td>
<td>
<input type="checkbox" name="Friday[0]" value="1">
</td>
<td>
<input type="button" class="btn btn-danger" value="Delete" onClick="deleteRow('dataTable')" />
</td>
</tr>
</tbody>
The code that i use for inserting the data to the database,
<?php
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
if ($_POST['startTime']) {
foreach ($_POST["startTime"] as $key => $value) {
$endTime = $_POST["endTime"][$key];
$monday = isset($_POST["Monday"][$key]) ? 1 : 0;
$tuesday = isset($_POST["Tuesday"][$key]) ? 1 : 0;
$wednesday = isset($_POST["Wednesday"][$key]) ? 1 : 0;
$thursday = isset($_POST["Thursday"][$key]) ? 1 : 0;
$friday = isset($_POST["Friday"][$key]) ? 1 : 0;
$sql = "INSERT INTO timetableschedule ( startTime, endTime, Monday, Tuesday, Wednesday, Thursday, Friday) " .
"VALUES ('$value', '$endTime', '$monday', '$tuesday', '$wednesday', '$thursday', '$friday')";
mysqli_query($con, $sql);
}
}
echo "1 record added";
mysqli_close($con);
}
?>
Suppose i submit the form the way i showed in the above image, i gotten this result in the database,
The value that suppose to be with the time "1600 1700" appeared on the "1400 1500" row. I would want the values to be saved accordingly as selected.
Aucun commentaire:
Enregistrer un commentaire