前端mysql视图,用复选框删除条目?

时间:2012-06-19 23:44:19

标签: php mysql onclick delete-row

我正在建立一个我之前提出过并已经解决的问题:front end mysql, deleting a row

基本上,我有一个用户可以查看数据库的前端。我希望有一个可以为多行选择的复选框,而不是每行旁边都有一个删除按钮。然后,用户只需单击一个删除按钮,即可删除所有选定的行。我根本不了解很多php和mysql,所以我不知道如何处理我已有的代码。

目前,onclick调用了删除功能。有人可以帮忙吗?

我有一个php文件,它将mysql数据的html输出为long strong,我需要更改的部分是:

$display_string .= "<td class='blank'><input type=\"button\" VALUE=\"Delete\" onclick='delFunction(" . $row['ID'] . ")' ></td>";

接下来我的删除功能:

function delFunction(ID){
    // confirm delete
    if (!confirm(\"Are you sure you want to delete?\")) return false;

    var ajaxRequest;  // The variable that makes Ajax possible!


    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject(\"Msxml2.XMLHTTP\");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject(\"Microsoft.XMLHTTP\");
            } catch (e){
                // Something went wrong
                alert(\"Your browser broke!\");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            var ajaxDisplay = document.getElementById('ajaxDiv');
            ajaxDisplay.innerHTML = ajaxRequest.responseText;
        }
    }

    var queryString = \"?ID=\" + ID


    ajaxRequest.open(\"GET\", \"delete_row.php\" + queryString, true);
    ajaxRequest.send(null); 
}

1 个答案:

答案 0 :(得分:0)

根据我对你的问题的理解,我发布了前端和后端的一些代码

前端示例代码

<body>
<form action="delete.php" method="post">
<input type="checkbox" name="del_chk[]">Item1
<input type="checkbox" name="del_chk[]">Item2
<input type="checkbox" name="del_chk[]">Item3
<input type="checkbox" name="del_chk[]">Item4
.
.
<input type="submit">
</form>

................

您的后端代码现在是......

<?php
if(isset($_POST['del_chk'])){
$chkbox=$_POST['del_chk'];
foreach($chkbox as $key=>$value) {

//Now you can get your value and use it in mysql delete statements


//for example
$del_query="DELETE FROM `yourtable` WHERE `pri_key`=$value;";
if(mysql_query($del_query)) {
echo "Successful Deletion of: ".$value;
}
else 
{
echo "Unsuccessful Deletion of: ".$value;
} 

} //end foreach
}
?>

我不太了解ajax。但你可以使用ajax来调用这个页面..