多个复选框,用PHP分隔提交按钮

时间:2015-02-25 19:27:55

标签: php mysql database function codeigniter

您好,感谢您查看我的问题。希望你能帮助我。我遇到了问题。我想在复选框中分隔我的提交按钮。由于提交复选框的按钮位于网站的上半部分。这是我的代码段: 这是按钮的代码。我正在使用codeigniter

          <button name="delete" class="label label-danger" type="submit" value ="delete">Remove</button>

所以我希望每次点击按钮时,复选框都会被删除。

以下是复选框的代码:

            <?php
                $con=mysqli_connect("127.0.0.1","root","","dbms_project");
                // Check connection
                    if (mysqli_connect_errno())
                    {
                    echo "Failed to connect to MySQL: " . mysqli_connect_error();
                    }
                $result = mysqli_query($con,"SELECT * FROM customers");
                while($row = mysqli_fetch_array($result))
                {
                    $id = $row['Customer_ID'];
                    echo "<tr>";
                    echo "<tr>";
                    echo "<td>" . form_checkbox('checkbox[]', '$id', FALSE) . "</td>";
                    echo "<td>" . $id . "</td>";
                    echo "<td>" . $row['First_Name'] . "</td>";
                    echo "<td>" . $row['Last_Name'] . "</td>";
                    echo "<td>" . $row['Address'] . "</td>";
                    echo "<td>" . $row['Phone'] . "</td>";
                echo "</tr>";
                    }

                    if (isset($_POST['delete'])) {
                        $checkbox = $_POST['checkbox'];
                        $count = count($checkbox);

                        for($i = 0; $i < $count; $i++) {
                            $id = (int) $checkbox[$i]; // Parse your value to integer

                            if ($id > 0) { // and check if it's bigger then 0
                                mysql_query("DELETE FROM customers WHERE customer_id = $id");
                            }
                        }
                    }
                ?>

1 个答案:

答案 0 :(得分:-1)

我已经解决了这个问题。看来我需要使用jquery函数。例如 : JQuery脚本。

            <script>
            $("#formDelete").click(function(){
            $("#formDeletion").trigger("submit");
            })
            </script>

表格:

 <form id='formDeletion' action ='remove' method='post'>
            <!--Start of calling the tables-->
            <?php
                $con=mysqli_connect("127.0.0.1","root","","dbms_project");
                // Check connection
                    if (mysqli_connect_errno())
                    {
                    echo "Failed to connect to MySQL: " . mysqli_connect_error();
                    }
                $result = mysqli_query($con,"SELECT * FROM customers");
                while($row = mysqli_fetch_array($result))
                {
                    $id = $row['Customer_ID']; ?>
                    <tr>
                    <td><input type='checkbox' name='selector[]' value='<?php echo $id?>'/></td>
                    <td><?php echo $id?> </td>
                    <td> <?php echo $row['First_Name'] ?></td>
                    <td> <?php echo $row['Last_Name'] ?> </td>
                    <td> <?php echo $row['Address']?> </td>
                    <td> <?php echo $row['Phone'] ?> </td>
                </tr>
                <?php 
                }?>
      </form>

输入按钮是formDelete,表单的id是formDeletion。

感谢大家的帮助!