更改并从列表中删除所选项目 - PHP

时间:2016-08-23 15:12:25

标签: 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按钮使用相同的数组。

1 个答案:

答案 0 :(得分:0)

在使用试错法并尝试两周后,我终于得到了解决方案,我有一个严重的抱怨,因为这个论坛上没有人愿意向我展示解决方案。

很简单。我所要做的只是使用$_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中没有人帮助过我。

相关问题