我正在制作一个网站,其中我要嵌入删除使用多个复选框的功能。这是我的代码。我的问题是1. Ajax调用不起作用。2.如何从数据库中查找数组。
<?php
if(isset($_POST['Delete']))
{
$array=$_POST['check_box'];
}
?>
<form method="post" id="form">
<table width="200" border="1">
<tr>
<td>select</td>
<td>NAme</td>
<td>Action</td>
</tr>
<?php
while($selectnumberarr=mysql_fetch_array($selectnumber))
{
?>
<tr>
<td><input type="checkbox" name="check_box[]" class="check_box" id="<?php $selectnumberarr[0]; ?>" /> </td>
<td><?php echo $selectnumberarr[1]; ?></td>
</tr>
<?php
}?>
<input type="submit" name="Delete" id="delete">
</table>
</form>
下面是我的ajax和javascript代码。
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#delete').click(function() {
$.ajax({
type: "POST",
url: "checkbox.php",
data: $('#form').serialize(),
cache: false,
success: function(html)
{
alert("true");
}
});//end ajax
});
});
</script>
任何帮助都会得到重视
发布于 2013-12-12 07:36:14
您的代码几乎是正确的。你需要去掉`onChange="show()“for input复选框,因为如果你有jquery,你就不需要把事件放在HTML元素上。
使用jquery 'on'方法来兼容最新的php库。
用下面的jquery代码替换你的jquery代码:
<script>
$(document).ready(function(){
$('#delete').on('click',function()
{
var cat_id = $('.check_box:checked').map(function() {
return this.id;
}).get().join(',');
console.log(cat_id);
$.ajax({
type: "POST",
url: "checkbox.php",
data: { "kw ":cat_id },
datatype:'json',
success: function(html)
{
alert("true");
}
});//end ajax
});
});
</script>
在jquery中使用".check_box“而不是"element”,以避免选中所有复选框,而不是选中所需的复选框。
希望能有所帮助。
发布于 2013-12-12 06:50:59
为什么不使用数组来发送复选框,比如:
HTML部件:
<?php
if (isset($_POST['check_box'])) {
var_dump($_POST['check_box']);
echo "ajax call is working";
}
?>
<form id="form">
<table width="200" border="1">
<tr>
<td>select</td>
<td>NAme</td>
<td>Action</td>
</tr>
<?php
while ($selectnumberarr = mysql_fetch_array($selectnumber)) {
?>
<tr>
<td><input type="checkbox" name="check_box[]" class="check_box" value="<?php echo $selectnumberarr[0]; ?>" /> </td>
<td><?php echo $selectnumberarr[1]; ?></td>
</tr>
<?php
}
?>
</table>
<input type="button"name="delete" id="delete" value="Delete" />
</form>
JQuery部件:
<script type="text/javascript">
$(document).ready(function(){
$('#delete').click(function() {
$.ajax({
type: "POST",
url: "checkbox.php",
data: $('#form').serialize(),
cache: false,
success: function(html)
{
alert("true");
}
});//end ajax
});
});
</script>
所以你可以很容易的在php中得到一个选中的复选框数组:
$idsArray = $_POST["check_box"];
现在看起来是这样的:
array(
"1", "2","etc.."
);
因此,此数组包含要删除的选定复选框的所有ids。
https://stackoverflow.com/questions/20536427
复制相似问题