【发布时间】:2013-11-21 06:04:53
【问题描述】:
在我的应用程序中,我在表格中显示数据库内容。对于显示的每一行,我在行尾添加一个复选框:
echo '<td><input type="checkbox" name="ticked[]"></td>';
当用户勾选了他们希望删除条目的许多框时,他们点击这个删除按钮(前端是 zurb 基础框架):
<a href="#" class="button radius expand" id="deleteUrl" name="deleteUrl" onClick="deleteUrl('deleteUrl');return false;">Delete URL</a>
当这个按钮被按下时,会触发 deleteUrl ajax 函数:
function deleteUrl(str)
{
document.getElementById("content01").innerHTML="";
if (str=="")
{
document.getElementById("content01").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("content01").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","deleteUrl.php?deleteUrl="+str,true);
xmlhttp.send();
document.getElementById("content02").innerHTML = 'Your URL was successfully deleted!<br/><br/>';
xmlhttp.onreadystatechange = urlRefresh;
return;
}
ajax 函数将进程定向到我的 deleteUrl.php 文件:
<!--Include Database connections info-->
<?php include('config.php'); ?>
<?php
$deleteUrl = $_GET ['$deleteUrl'];
if(isset($_GET['delete'])) {
if(is_array($_GET['url'])) {
foreach($_GET['url'] as $id) {
$query = "DELETE FROM contact WHERE url=". $url;
mysql_query($query)or die(mysql_error());
}
}
}
mysql_close();
?>
到目前为止,该过程运行完成,没有错误。但是,检查的条目在此过程中不会被删除。
问题:我需要做什么才能使用复选框使删除过程正常工作?
编辑代码:
function runDelete(str, id)
xmlhttp.open("GET","deleteUrl.php?deleteUrl="+str+"&ticked="+id,true);
<a href="#" class="button radius expand" id="deleteUrl" name="deleteUrl" onClick="runDelete('deleteUrl', id);return false;">Delete URL</a>
echo '<td><input type="checkbox" name="ticked[]" value="'.$row['id'].'"></td>';
【问题讨论】:
-
您应该先看到发布的内容。我会使用 Mozilla FF 和 fireBug 来查看请求和响应。当我看到您的代码时,它不会发送要删除的数组。您只需发送到您的 PHP '删除 url'
标签: php ajax checkbox sql-delete