PHP使用复选框

时间:2017-10-06 04:16:34

标签: javascript php mysql datatables

我正在尝试使用mysql从php数据表中删除多个选定的行。但是接收以下print_r($_POST);返回包含2行数据:

  

数组([user_payment_request_length] => 10 [checked_id] =>数组(   [0] => [1] => )[select_all] => [bulk_delete_submit] => )

没有数据传递使mysql程序成功。特别是$pr_id是一个整数。当我尝试按如下方式打印时:

foreach ($idArrr as $pr_id) {
echo $pr_id;
}

显示数组到字符串转换错误。 这是我的完整代码:

   $(document).ready(function () {
                            $('input').val('');
                        });
                        $(document).ready(function () {
                            $('#user_payment_request').DataTable();
                        });

                        function deleteConfirm() {
                            var result = confirm("Are you sure to delete?");
                            if (result) {
                                return true;
                            } else {
                                return false;
                            }
                        }

                        $(document).ready(function () {
                            $('#select_all').on('click', function () {
                                if (this.checked) {
                                    $('.checkbox').each(function () {
                                        this.checked = true;
                                    });
                                } else {
                                    $('.checkbox').each(function () {
                                        this.checked = false;
                                    });
                                }
                            });

                            $('.checkbox').on('click', function () {
                                if ($('.checkbox:checked').length == $('.checkbox').length) {
                                    $('#select_all').prop('checked', true);
                                } else {
                                    $('#select_all').prop('checked', false);
                                }
                            });
                        });
$stmt_payment = $mysqli->prepare('SELECT pr_id,pr_rqtime,pr_amount,pr_processor,pr_status,pr_date FROM payment WHERE user_id = ? ');
                        $stmt_payment->bind_param('i', $user_id);
                        $stmt_payment->execute();    // Execute the prepared query.
                        $stmt_payment->store_result();
                        $stmt_payment->bind_result($pr_id, $pr_rqtime, $pr_amount, $pr_processor, $pr_status, $pr_date);
                        ?>
                        <form name="payment_table" method="post" onsubmit="return deleteConfirm();">
                            <table id="user_payment_request" class="table table-responsive table-hover" cellspacing="0" width="100%">
                                <thead>
                                    <tr>
                                        <th>Payment ID </th>
                                        <th>Payment Request Time</th>
                                        <th>Amount</th>
                                        <th>Processor</th>
                                        <th>Status</th>
                                        <th>Payment Date</th>
                                        <th>Select</th>
                                    </tr>
                                </thead>

                                <?php
                                while ($stmt_payment->fetch()) {
                                    ?>
                                    <tbody>
                                        <tr>
                                            <td> <?php echo $pr_id; ?> </td>    
                                            <td> <?php echo $pr_rqtime; ?> </td>
                                            <td> <?php echo $pr_amount; ?> </td>
                                            <td> <?php echo $pr_processor; ?></td>
                                            <td><?php echo $pr_status; ?> </td>
                                            <td><?php echo $pr_date; ?></td>
                                            <td> <input type="checkbox" name="checked_id[]" class="checkbox" value="<?php echo $pr_id; ?>"/></td>  
                                        </tr>
                                        <?php
                                    }
                                    ?>
                                </tbody>
                            </table>
                            <strong>Select All <input type="checkbox" name="select_all" id="select_all" value=""/> </strong> 
                            <button type="submit" class="btn btn-danger" name="bulk_delete_submit">Delete</button>
                        </form>
                        <?php
                        //----Delete record part
                        print_r($_POST);
                        if (isset($_POST['bulk_delete_submit'])) {
                            $idArrr[] = $_POST['checked_id'];
                            foreach ($idArrr as $pr_id) {
                                $stmt_delete_payment = $mysqli->prepare("CALL user_delete_payment(?,?,?,?,?)");
                                $stmt_delete_payment->bind_param('iisds', $pr_id, $user_id, $pr_rqtime, $pr_amount, $pr_processor);
                                if (!$stmt_delete_payment->execute()) {
                                    echo '<script>swal("Error!", "Database error deleting payout request, please try again", "error");</script>';
                                    echo '<script>setTimeout(function(){location.href = "withdraw.php";},2000); </script>';
                                }
                            }
                            $stmt_delete_payment->close();
                            echo '<script>location.href = "withdraw.php";</script>';
                        }
                        // end of delete records                       
                        $stmt_payment->close();
                        ?>

1 个答案:

答案 0 :(得分:0)

我弄清楚了,我还有一部分javascript代码:

                        $(document).ready(function () {
                        $('input').val('');
                    });

正在删除输入值。这就是代码无效的原因。 如果用户按下后退按钮

,我有额外的代码以删除表单的值