0

我正在尝试使用复选框从表中删除所有记录。当用户选中最上面的复选框时,它将选中循环内的所有其他复选框,然后会出现一个关于删除记录的确认框。如果用户点击OK,所有的记录将使用$.ajax 删除,如果他点击Cancel,那么页面将返回相同的状态,并且不再选中复选框。

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

查询

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

4 回答 4

1

把它放在你的 if 块中。

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

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

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

于 2013-06-29T10:39:01.520 回答
0

You can use something like this in your HTML check all page

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

and use something like this in your PHP script

<?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;
?>
于 2013-06-29T18:36:00.150 回答
0

这里是 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;
            }
        });
    }
于 2013-06-29T10:57:54.570 回答
-1
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一些错误

于 2013-06-29T10:43:26.833 回答