繁体   English   中英

确认为真后,如何使用checkBox clickall从表中删除所有条目? jQuery Ajax PHP

[英]How to delete all entry from table using checkBox clickall after confirmation is true? jQuery Ajax PHP

我正在尝试使用复选框从表中删除所有记录。 当用户选中最上方的复选框时,它将选中循环内的所有其他复选框,然后将出现一个有关删除记录的确认框。 如果用户单击“确定”,则将使用$ .ajax删除所有记录,如果他单击“取消”,则页面将返回相同状态,并且不再选中该复选框。

<?php
        include 'dbconn.php';
    ?>
    <table border="1" >
    <tr><td align="center" width="20"><input type="checkbox" name='checkALL' id='checkALL'></td><td>Name</td>
    </tr>
    <?php
        $sql=mysql_query("SELECT * FROM names ORDER BY names ASC") or die(mysql_error());
        while($rows=mysql_fetch_assoc($sql)){
    ?>
        <tr>
            <td><input type="checkbox" name="id[]" id="id[]" value="<?php print $rows['id'];?>">
            </td><td>Name</td>
        </tr>
    <?php
    }
    ?>
    </table>

JQUERY

<script>
    $(function(){
    //click all 
    $('#checkALL').click(function(){
            $(':checkbox').attr({checked: 'true'});
            var del=confirm("You checked all the box. Delete All?");
            if(del==true){
                //delete here using $.ajax              
            }
            else{
                window.location.reload(false);
                $('#checkAll').attr({checked: 'false'});
            }
        });
    });
</script>

将此放置在您的if块中。

$.ajax({
type: "GET",
url: '<php file which truncates table>',

success: function (data) {
    if (data == 'truncated') {
        alert('success');
    } else {
        alert('not truncated');
    }
}
});

成功返回从您的php文件截断的字符串。

在这里U- PageUrl,A-页面上的操作,P-参数

if (confirm('Are you sure to delete this record?')) {
 Operation(U, A, P);
}
    function Operation(U, A, P) {
        $.ajax({
            type: "POST",
            url: U + '/' + A,
            data: P,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            cache: false,
            success: function (r) {
                var str = r.d;
            }
        });
    }

您可以在HTML检查所有页面中使用类似的内容

<html>
<head><title>Select/Delete ALL with jQuery/PHP</title>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>

<body>

<table border="1" cellpadding="5">
    <tr>
        <th>
            <input type="checkbox" id="checkAll" />
        </th>
    </tr>

    <tr>
        <td><input type="checkbox" class="check" name="posts[]" value="100" /></td>
    </tr>

    <tr>
        <td><input type="checkbox" class="check" name="posts[]" value="200" /></td>
    </tr>

    <tr>
        <td><input type="checkbox" class="check" name="posts[]" value="300" /></td>
    </tr>

</table>

<script>
$(document).ready(function(){
    var checked = false;
    $('#checkAll').click(function(){
        if (checked == false){
            checked = true
        } else {
            checked = false
        }

        $('input.check').attr("checked",checked);

        var confirmDelete=confirm("You checked all the box. Delete All?");

        if (confirmDelete==true){

            var csv = '';           
            $('.check').each(function() {
                csv += $(this).attr("value") + ",";
            });

            $.ajax({
                type: "POST",
                url: "delete.php",
                data: { tobeDeleted: csv }
            }).done(function( msg ) {
                console.log( "Data has been deleted: " + msg );
            });

        }  else {
        $('#checkAll').attr("checked", false);
        $('input.check').attr("checked", false);
    }
    });
});

</script>

</body>
</html>

并在您的PHP脚本中使用类似的内容

<?php
$postDeleted = substr($_POST['tobeDeleted'], 0, strlen($_POST['tobeDeleted'])-1);
$arrDeleted = explode(",", $postDeleted);

$sql = "DELETE FROM employee WHERE 1=1 ";
foreach($arrDeleted as $key=>$value){
    $sql .= "OR employee_id = $value ";
}

echo $sql;
?>
if(del==true){    
  $.ajax({
                    type: "POST",
                    url: 'your_file_url',
                    async:false,

                    cache: false,
                    success: function(data){
                       if (data == 1) {
        alert('success');
    } else {
        alert('Error');
    } 

                    }
                });
}

在您的php文件中,回显1表示成功删除,回显0表示某些错误

暂无
暂无

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

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