我正在创建一个涉及复选框的表单。连续有5个复选框。如果用户添加了另一行,则会有另外5个复选框。
我使用的代码,
<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>
&#13;
我用于将数据插入数据库的代码
<?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);
}
?>
&#13;
假设我按照我在上图中显示的方式提交表单,我在数据库中得到了这个结果,
假设与时间相关的值&#34; 1600 1700&#34;出现在&#34; 1400 1500&#34;行。我希望相应地保存值。
答案 0 :(得分:1)
编辑:删除零(name="Monday[]"
)无法解决问题,因为未提交未选中的广播或复选框元素。
我会考虑在生成行时为您的复选框名称添加索引,例如:
<input type="checkbox" name="Monday[0]" value="1">
<input type="checkbox" name="Tuesday[0]" value="1">
...
<input type="checkbox" name="Monday[1]" value="1">
<input type="checkbox" name="Tuesday[1]" value="1">
...
<input type="checkbox" name="Monday[2]" value="1">
<input type="checkbox" name="Tuesday[2]" value="1">
这样你的PHP循环应该可以工作
答案 1 :(得分:1)
JSON方式:
function sendFunction()
{
//tr
var checkboxJSON = {};
var i = 0;
var tr = $("#dataTable > tr").each(function(){
//checkboxes
checkboxJSON[i] = {};
$(this).find("input[type='checkbox']").each(function(){
if ($(this).is(':checked'))
{
checkboxJSON[i][$(this).attr("name")] = 1;
}
else
{
checkboxJSON[i][$(this).attr("name")] = 0;
}
});
i++;
});
var checkboxData = JSON.stringify(checkboxJSON); //send this to the server as checkboxdata!
console.log(checkboxData);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<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" value="1">
</td>
<td>
<input type="checkbox" name="Tuesday" value="1">
</td>
<td>
<input type="checkbox" name="Wednesday" value="1">
</td>
<td>
<input type="checkbox" name="Thursday" value="1">
</td>
<td>
<input type="checkbox" name="Friday" value="1">
</td>
<td>
<input type="button" class="btn btn-danger" value="Delete" onClick="deleteRow('dataTable')" />
</td>
</tr>
<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" value="1">
</td>
<td>
<input type="checkbox" name="Tuesday" value="1">
</td>
<td>
<input type="checkbox" name="Wednesday" value="1">
</td>
<td>
<input type="checkbox" name="Thursday" value="1">
</td>
<td>
<input type="checkbox" name="Friday" value="1">
</td>
<td>
<input type="button" class="btn btn-danger" value="Delete" onClick="deleteRow('dataTable')" />
</td>
</tr>
</tbody>
</table>
<input type="button" class="btn btn-danger" value="Send" onClick="sendFunction('dataTable')" />
为您的解决方案。工作:
<input type="hidden" value="{}" name="checkboxes" />
<form id="formDates" action="file.php" method="post" onSubmit="checkForm" />
function checkForm(e)
{
e.preventDefault(); //prevents form from being sent to early
var checkboxJSON = {};
var i = 0;
var tr = $("#dataTable > tr").each(function(){
//checkboxes
checkboxJSON[i] = {};
$(this).find("input[type='checkbox']").each(function(){
if ($(this).is(':checked'))
{
checkboxJSON[i][$(this).attr("name")] = 1;
}
else
{
checkboxJSON[i][$(this).attr("name")] = 0;
}
});
i++;
});
$("input[type='hidden']").val(JSON.stringify(checkboxJSON)); //save this to the hidden input.
document.getElementById("formDates").submit(); //submit all the data.
}
json_decode()
$checkboxes = json_decode($_POST['checkboxes'], true); //use true to set it as an array.
foreach ($_POST["startTime"] as $key => $value) {
$endTime = $_POST["endTime"][$key];
$monday = $checkboxes[$key]['Monday']; //key should refer to an INT. 0 for the first row.
$tuesday = $checkboxes[$key]['Tuesday'];
$wednesday = $checkboxes[$key]['Wednesday'];
$thursday = $checkboxes[$key]['Thursday'];
$friday = $checkboxes[$key]['Friday'];
$sql = "INSERT INTO timetableschedule ( startTime, endTime, Monday, Tuesday, Wednesday, Thursday, Friday) " .
"VALUES ('$value', '$endTime', '$monday', '$tuesday', '$wednesday', '$thursday', '$friday')";
mysqli_query($con, $sql);
}