在datatables php中获取复选框字段的问题
我正在用PHP处理数据表 我在第一列有一个表,有一个复选框,上面写着全选,每一行都有一个复选框。 当用户单击“全选”复选框时,在单击“提交”后,将只提取并在php中显示可见行的复选框值 问题是,当我从第2页选择一个复选框时,我说我已经在澳大利亚用id 13进行了检查,然后我转到第3页,什么也没有选择。然后单击submit i don’t get the$u POST字段名与澳大利亚is 但是当你在澳大利亚登陆并点击submit而不改变页面时,它会给我它的$u帖子详细信息。 我想知道的是,当我从不同的页面中选择了多个复选框时,他们是如何将复选框数据发送到php的 我正在使用最新的datatable js文件 图像用于让开发人员理解 下面是我的HTML代码 view.php在datatables php中获取复选框字段的问题,php,jquery,datatable,datatables,datatables-1.10,Php,Jquery,Datatable,Datatables,Datatables 1.10,我正在用PHP处理数据表 我在第一列有一个表,有一个复选框,上面写着全选,每一行都有一个复选框。 当用户单击“全选”复选框时,在单击“提交”后,将只提取并在php中显示可见行的复选框值 问题是,当我从第2页选择一个复选框时,我说我已经在澳大利亚用id 13进行了检查,然后我转到第3页,什么也没有选择。然后单击submit i don’t get the$u POST字段名与澳大利亚is 但是当你在澳大利亚登陆并点击submit而不改变页面时,它会给我它的$u帖子详细信息。 我想知道的是,当我
<
?php
require 'conn.php';
$sql = "SELECT * FROM tbl_country";
$result = $conn->query($sql);
$country_array = array();
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
$country_array[] = $row;
}
} else {
echo "0 results";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
</head>
<body>
<br>
<br>
<div class="container">
<form action="process.php" method="post">
<input type="submit" name="submit" value="submit">
<table class="table table-bordered" id="example" >
<thead>
<tr>
<th>
<label for="select_all">
<input type="checkbox" id="select_all" onclick="selectAll()" name="select_all" value="1" > Select All
</label>
</th>
<th>id</th>
<th>name</th>
</tr>
</thead>
<tbody>
<?php
foreach ($country_array as $value) {
?>
<tr>
<td>
<label for="select_one_<?php echo $value['country_id']; ?>">
<input type="checkbox" class="common_class" id="select_one_<?php echo $value['country_id']; ?>" name="select_one_<?php echo $value['country_id']; ?>" value="select_one_<?php echo $value['country_id']; ?>">
</label>
</td>
<td><?php echo $value['country_id']; ?></td>
<td><?php echo $value['country_name']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js">
</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js">
</script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js">
</script>
<script>
$(document).ready(function () {
$('#example').DataTable({
"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]]
});
});
function selectAll() {
var is_checked = document.getElementById('select_all').checked;
if (is_checked) {
$('.common_class').prop('checked', true);
} else {
$('.common_class').prop('checked', false);
}
console.log(is_checked);
}
</script>
</body>
</html>
PHP文件process.PHP代码
<?php
echo '<pre>';
print_r($_POST);
?>
php文件conn.php
var frmData = $("#my_form").serializeArray(); //my_form the id of your form
$(".common_class").each(function(){
frmData.push({name: "country_selection[]", value: $(this).val()});
});
$.ajax({
url: "your_action_url",
type: 'POST',
data: frmData,
cache: false,
success: function(result) {
},
error: function(result) {
}
});
提前感谢使用serializeArray和ajax提交选中/未选中的所有复选框
$checkbox = $_POST['common_selection'];
for($i=0;$i<count($checkbox);$i++){
echo $checkbox[$i];
}
这将发布所有表单字段和名为country_selection[]的新对象数组
在php中,您现在可以使用
$('#deleteTriger').on("click", function(event){ // triggering delete one by one
if( $('.deleteRow:checked').length > 0 ){ // at-least one checkbox checked
var ids = [];
$('.deleteRow').each(function(){
if($(this).is(':checked')) {
ids.push($(this).val());
}
});
var ids_string = ids.toString(); // array to string conversion
console.log(ids_string);
$.ajax({
type: "POST",
url: "/getIds.php",
data: {data_ids:ids_string},
success: function(result) {
table.draw(); // redrawing datatable
},
async:false
});
}
});
在php文件中,获取所有选定的ID,如$data\u id=$\u POST['data\u id'] 可能重复的@Terry感谢您的快速响应,假设用户已选中其中一个复选框,但想取消选中该复选框,我应该为此提供一个额外的按钮,还是他们的解决方案更好。
$('#deleteTriger').on("click", function(event){ // triggering delete one by one
if( $('.deleteRow:checked').length > 0 ){ // at-least one checkbox checked
var ids = [];
$('.deleteRow').each(function(){
if($(this).is(':checked')) {
ids.push($(this).val());
}
});
var ids_string = ids.toString(); // array to string conversion
console.log(ids_string);
$.ajax({
type: "POST",
url: "/getIds.php",
data: {data_ids:ids_string},
success: function(result) {
table.draw(); // redrawing datatable
},
async:false
});
}
});