Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
确认为真后,如何使用复选框clickall从表中删除所有条目?jqueryajaxphp_Php_Jquery_Ajax_Checkbox - Fatal编程技术网

确认为真后,如何使用复选框clickall从表中删除所有条目?jqueryajaxphp

确认为真后,如何使用复选框clickall从表中删除所有条目?jqueryajaxphp,php,jquery,ajax,checkbox,Php,Jquery,Ajax,Checkbox,我正在尝试使用复选框从表中删除所有记录。当用户选中最上面的复选框时,它将选中循环中的所有其他复选框,然后会出现一个关于删除记录的确认框。如果用户单击“确定”,则将使用$.ajax删除所有记录;如果用户单击“取消”,则页面将返回到相同的状态,并且不再选中复选框 <?php include 'dbconn.php'; ?> <table border="1" > <tr><td align="center" width

我正在尝试使用复选框从表中删除所有记录。当用户选中最上面的复选框时,它将选中循环中的所有其他复选框,然后会出现一个关于删除记录的确认框。如果用户单击“确定”,则将使用$.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


把这个放在你的if块中

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

success: function (data) {
    if (data == 'truncated') {
        alert('success');
    } else {
        alert('not truncated');
    }
}
});
成功返回php文件中截断的字符串

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文件中,echo 1表示成功删除,0表示某些错误,这里是U-PageUrl,A-Action on-page,p-Parameter

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

嗨,谢谢你的回答,不过,请再读一遍我的问题。我的复选框功能好吗?我只是在学习jquery,我不知道我是否有正确的代码。更清楚你需要什么?我很肯定我的回答满足了你的问题。
<?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;
?>