【问题标题】:Front end mysql view, delete entry with checkbox?前端mysql视图,用复选框删除条目?
【发布时间】:2012-06-19 23:44:18
【问题描述】:

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

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

目前,onclick 调用了一个删除函数。有人可以帮忙吗?

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

$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); 
}

【问题讨论】:

    标签: php mysql onclick delete-row


    【解决方案1】:

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

    前端示例代码

    <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来调用这个页面..

    【讨论】:

    • 此外,您的 ajax 中的引号被转义。为什么?你不必那样做!
    • 每个checkbox的ID是怎么传的?对我来说,传递的只是值'on',使用以下复选框代码: $display_string .= "";
    • 我在这里为同样的问题写了一个解决方案:stackoverflow.com/questions/11119719/…
    猜你喜欢
    • 2013-11-21
    • 1970-01-01
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 2014-09-18
    • 2016-12-01
    • 2021-01-22
    相关资源
    最近更新 更多