dongxuan2577 2012-06-19 23:44
浏览 30
已采纳

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

I'm building off of a question I had asked and resolved earlier: front end mysql, deleting a row

Basically, I've got a front end where users can view a DB. Instead of having a delete button next to each row, I'd like to have a checkboxes that can be selected for multiple rows. Then, the user only clicks a single delete button and all the selected rows are removed. I don't know much php and mysql at all, so I'm not sure how to approach the code that I already have.

Currently, the onclick calls a delete function. Can anyone help?

I've got a php file that outputs the html for the mysql data into a long strong, the part I need to change is:

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

Next my delete function:

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条回答 默认 最新

  • douli1854 2012-06-20 02:04
    关注

    To my understanding of your problem, I am posting some codes for both front and back ends

    Front-End Sample Code

    <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>
    

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

    Your back-end code would now be...

    <?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
    }
    ?>
    

    I don't know much of ajax. but you can use ajax to call this page..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂