更改和删除选定的项目从列表- PHP


change and delete selected items from list - PHP

我已经建立了一个网站,其中有很多列表/表(基于php)。我正在尝试为列表制作一个Change AllDelete All系统。我已经将checkbox添加到所有单独的列表项目中。我也有一个复选框,选择所有这些项目与单击。现在我被困在如何执行更改和删除选项。已经3天了……

  • 我已经做了Change All代码正确。但是我不明白Delete All部分。
  • 我不知道通过$targetpage变量使它重定向之后。

代码如下:

<!DOCTYPE html>
<html>
<title>List 1</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css">
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3-theme-purple.css">
<?php include('dbcon.php');?>
<style>a {text-decoration: none;}</style>
<!--Select All-->
<script>
 function checkAll(ele) {
     var checkboxes = document.getElementsByTagName('input');
     if (ele.checked) {
         for (var i = 0; i < checkboxes.length; i++) {
             if (checkboxes[i].type == 'checkbox') {
                 checkboxes[i].checked = true;
             }
         }
     } else {
         for (var i = 0; i < checkboxes.length; i++) {
             console.log(i)
             if (checkboxes[i].type == 'checkbox') {
                 checkboxes[i].checked = false;
             }
         }
     }
 }
</script>
<body>
<!--?php include ('header.php');?-->
<form name="form1" enctype="multipart/form-data" action="ajaxAction.php" method="post">
<div class="w3-container">
    <div class="w3-padding-32 w3-center">
        <input class="w3-btn w3-green" type="submit" name="chkbox" value="Change All" />
        <input class="w3-btn w3-red" type="submit" value="Delete All">
    </div>
<center>
<table class="w3-table-all" style="width: auto;">
    <tr class="w3-theme">
        <th><input class="w3-check" type="checkbox" onchange="checkAll(this)" name="chk[]" /></th>
        <th>Data</th>
    </tr>
    <tr>
        <td>
            <input class="w3-check" type="checkbox" name="chkbox[]" />
        </td>
        <td class="w3-small" >
            The data is displayed here.
        </td>
    </tr>
</table>
</center>
</div>
</form>
<br>
<!--?php include ('footer.php');?-->
</body>
</html>
下面是ajaxAction.php代码:
<!--Change-->
    <?php
    if ($_REQUEST['chkbox']) {
        $targetpage = $_REQUEST['targetpage'];
        $change = array ();
        $change = $_REQUEST['chkbox'];
        for($c=0;$c<count($change); $c++){
            $sql_all = "update `table` set `value`='123' WHERE `id` = '$change[$c]'";
            mysql_query($sql_all) or die(mysql_error());
        }
    ?>
    <script>
        var targetpage = "<?php echo $targetpage ?>";
        location.href=targetpage+"?done";
    </script>
    <?php } ?>
    <!--Delete-->
    <?php ?>
    <script>
        var targetpage = "<?php echo $targetpage ?>";
        location.href=targetpage+"?done";
    </script>

使用我当前的代码,我只能使用所选数组chkbox[]进行一个处理,即Change All。我不知道如何使用相同的数组Delete All按钮

在使用试错法和尝试两周后,我终于自己找到了解决方案,我有一个严重的抱怨,因为这个论坛没有人关心告诉我解决方案。

很简单。我所要做的就是使用$_POST而不是$_REQUEST,并使用不同的名称,像这样:

<input class="w3-btn w3-green" type="submit" name="submit1" value="Change All">
<input class="w3-btn w3-red" type="submit" name="submit2" value="Delete All">
所以,我把ajaxAction.php改成:
<?php
//Change All
if(isset($_POST['submit1'])){
    if(!empty($_POST['chkbox'])){
        $targetpage = $_REQUEST['targetpage'];
        foreach($_POST['chkbox'] as $selected){
            $sql_all = "update `table` set value='123' WHERE `id` = '$selected'";
            mysql_query($sql_all) or die(mysql_error());
        }
    }
?>
<script>
    location.href="tables.php?done";
</script>
<?php } ?>

<?php
//Delete All
if(isset($_POST['submit2'])){
    if(!empty($_POST['chkbox'])){
        foreach($_POST['chkbox'] as $selected){
            $sql_all = "DELETE FROM `table` WHERE `id` = '$selected'";
            mysql_query($sql_all) or die(mysql_error());
        }
    }
?>
<script>
    location.href="tables.php?done";
</script>
<?php } ?>

你所要做的就是告诉我应该使用$_POST。Stack Overflow没有人帮我。