Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.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
使用PHP和JavaScript单击复选框更新帖子状态时出现问题_Javascript_Php_Html_Mysql - Fatal编程技术网

使用PHP和JavaScript单击复选框更新帖子状态时出现问题

使用PHP和JavaScript单击复选框更新帖子状态时出现问题,javascript,php,html,mysql,Javascript,Php,Html,Mysql,我有以下tbu帖子table: +---------+-----------------------------+-----------------------------+-----------------+-------------+ | post_id | post_title | post_url | post_sort_order | post_status | +---------+---------------

我有以下
tbu帖子
table:

+---------+-----------------------------+-----------------------------+-----------------+-------------+
| post_id |         post_title          |          post_url           | post_sort_order | post_status |
+---------+-----------------------------+-----------------------------+-----------------+-------------+
|       1 | Mooching in Mooloolaba      | mooching-in-mooloolaba      |               1 |           1 |
|       2 | Back in Townsville          | back-to-the-boat            |               2 |           1 |
|       3 | Re Naming                   | re-naming                   |               3 |           0 |
|       4 | Henry                       | henry                       |               4 |           1 |
|       5 | Magnetic Island             | magnetic-island             |               5 |           0 |
|       6 | Arriving in the Whitsundays | arriving-in-the-whitsundays |               6 |           1 |
|       7 | Back in Townsville          | townsville                  |               7 |           1 |
|       8 | Headboards                  | improving-the-cabins        |               8 |           1 |
|       9 | A great weekend             | a-great-weekend             |               9 |           1 |
|      10 | Headboard                   | headboard                   |              10 |           1 |
+---------+-----------------------------+-----------------------------+-----------------+-------------+
以下是我在
index.PHP
中的PHP代码:

<?php
$query = "SELECT * FROM `tb_posts`";
$rs = mysqli_query($con, $query);
// If at least one record found
if(mysqli_num_rows($rs) > 0)
{
?>
    <form name="frm1" method="get" action="">
        <table border="1" width="50%" align="center" cellspacing="0">
            <tr>
                <th colspan="5"><h2>Posts</h2></th>
            </tr>
            <tr>
                <th>ID</th>
                <th>Title</th>
                <th>URL</th>
                <th>Order</th>
                <th>Status</th>
            </tr>
            <?php
            $srno = 1;
            // Fetch table rows
            while($row = mysqli_fetch_array($rs))
            {
                $post_id = $row['post_id'];
                $status = $row['post_status'] == 1 ? "checked='checked'" : '';
                echo "<tr>";
                echo "<td>" . $row['post_id'] . "</td>";
                echo "<td>" . $row['post_title'] . "</td>";
                echo "<td>" . $row['post_url'] . "</td>";
                echo "<td><input type='checkbox' name='chkstatus' value='" . $row['post_status'] . "' $status onClick='changeStatus($post_id, this.value);' /></td>";
                echo "</tr>";
            }
            ?>
        </table>
    </form>
<?php
}
?>
基本上,这里我是在数据库的HTML表中表示帖子。HTML表有一列
status
,每个post行都有一个复选框。如果
post\u状态
为1,则将选中相应的复选框,否则将取消选中该复选框(对于
post\u状态
=0)

我正试图通过JavaScript更新帖子状态。单击复选框后,其相应的帖子状态和帖子id将在通过JavaScript提交表单后发送到
update.php
。在
update.php
中,更新查询将相应地运行

但不知什么原因,当我尝试执行所有这些操作时,单击复选框后,页面将重定向到

update.php?chkstatus=1&chkstatus=1&chkstatus=0&chkstatus=1&chkstatus=1&chkstatus=1&chkstatus=1&chkstatus=1&chkstatus=1

然后出现一个空白屏幕。甚至连查询都没有打印出来。当我点击后退按钮时,点击复选框的post行不会被更新。我真的不明白是什么导致了这个问题


我哪里做错了?这种方法是好的(如果我们只考虑PHP和JavaScript)?有没有其他更好的方法或逻辑来无缝地完成这一切?

您正在将表单操作设置为
update.php

document.frm1.action = "update.php?id="+id+"&status="+status;
但是在提交时,表单会用表单的内容覆盖get参数,因为您在表单中使用了
method=“get”


尝试将表单设置为
method=“post”
,这样表单内容就不会干扰get参数。

将设置表单的操作并使用
location=“update.php?id=“+id+”&status=“+status我想你发现了我代码中的错误。我将表单的method属性更改为
post
,我的代码确实有效。我不明白是谁否决了你。
<?php
if(isset($_GET['id']) && isset($_GET['status']))
{
    $post_id = $_GET['id'];
    $post_status = $_GET['status'];
    $post_status = $post_status == 1 ? 0 : 1;

    echo $query = "UPDATE `tb_posts` SET `post_status` = $post_status WHERE `post_id` = $post_id";
    mysqli_query($con, $query);
    header("Location: index.php");
    exit();
}
?>
document.frm1.action = "update.php?id="+id+"&status="+status;