繁体   English   中英

如何使用Ajax调用复选框删除多个

[英]How to delete multiple using checkbox with Ajax call

我正在尝试使用ajax调用删除带有复选框的值,任何人都可以帮助我。

无法找到错误,没有任何东西,这是一个模板,我具有检查所有内置代码的功能,因此需要更改复选框的任何内置代码。这是我的列表:

<form id="stafflistForm">
<input type="hidden" name="checkedids" value="<?php echo $staffResults['id_staff']; ?>">
<button id="deleteChecked"><i class="fa fa-trash-o"></i></button>
</form>

这是我的Ajax脚本:

<script language="JavaScript">  
    $("#deleteChecked").click(function()
    {
        $("#delshowMessageDiv").hide();
        $("#delshowMessage").html('');
        $.ajax({
            url: "staffcontroller.php",
            method: "POST",
            data: { delData : $("#stafflistForm").serialize(), 'action':'delete'},
            dataType: "json",
            success: function (response) {
                if(response["success"]==true)
                {
                    $("#delshowMessageDiv").hide();
                    $("#delshowSuccessMessageDiv").show();
                    $("#delshowSuccessMessage").html(response["message"]);
                    }else{
                    $("#delshowMessageDiv").show();
                    $("#delshowMessage").html(response["message"]);
                }   
            },
            error: function (request, status, error) {
                $("#hshowMessageDiv").show();
                $("#hshowMessage").html("OOPS! Something Went Wrong Please Try After Sometime!");
            }
        });
        return false;
    });     
</script>

这是我的控制器页面:

else if($_REQUEST['action']=='delete'){
    $delids=explode(",",$_REQUEST["checkedids"]);
    $count=count($delids);
    for($i=0;$i<$count;$i++)
    {
        $delQuery= $conn->query("DELETE FROM os_staff WHERE id_staff=".$delids[$i]);
    }
        if($delQuery){
        $response['message'] = "<strong>Success!</strong>Staff Deleted Successfully.";
        $response['success'] = true;
        }else{
        $response['message'] = "<strong>Warning!</strong> Staff Not Deleted.Please Check Carefully..";
        $response['success'] = false;
    }
    echo json_encode($response);
    exit;
}

PHP脚本应通过以下方式设置正确的mime类型:

header('Content-type: application/json');

除此之外:为什么您要为错误和成功消息使用单独的DIV容器? 为什么不使用一个“ feedback” div,它获得一个CSS类,该类进行格式化(基于错误或成功)。

首先:如果您使用html属性ID乘以id =“ deleteChecked”,请不要使用它。 改用类选择器或数据属性。

这是一个小脚本,它向您展示如何改进代码。 那应该可以帮助您解决问题。

 $(document).ready(function() { $('.delete-user').on('click', function(e) { // do here your ajax // this is just example $(this).parents('tr').remove(); }); }); 
 .fa-trash-o { padding: 3px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td> <form class="stafflistForm"> <input type="hidden" name="checkedids" value="<?php echo $staffResults['id_staff']; ?>"> <button class="delete-user"><i class="fa fa-trash-o"></i></button> </form> </td> <td>Tom</td> </tr> <tr> <td> <form class="stafflistForm"> <input type="hidden" name="checkedids" value="<?php echo $staffResults['id_staff']; ?>"> <button class="delete-user"><i class="fa fa-trash-o"></i></button> </form> </td> <td>Peter</td> </tr> <tr> <td> <form class="stafflistForm"> <input type="hidden" name="checkedids" value="<?php echo $staffResults['id_staff']; ?>"> <button class="delete-user"><i class="fa fa-trash-o"></i></button> </form> </td> <td>Son Goku</td> </tr> <tr> <td> <form class="stafflistForm"> <input type="hidden" name="checkedids" value="<?php echo $staffResults['id_staff']; ?>"> <button class="delete-user"><i class="fa fa-trash-o"></i></button> </form> </td> <td>Gozilla</td> </tr> </table> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM