首页 > 解决方案 > PHP 表单操作不起作用且未更新查询字符串

问题描述

我正在尝试使用 MySQL 数据库和 PHP 为学校项目制作过滤器,但是每当我按下过滤器按钮时,它都不会执行操作,因此不会更新查询字符串。

它指的是我的主页的原因是因为这个块代码:

if (empty($_GET['page'])) {
    $_GET['page'] = 'home';
}

if (empty($routes[$_GET['page']])) {
    header('Location: index.php');
    exit();
}

这是没有$GET['page']通过的时候,它只会参考我的主页。

所以问题可能出在我的表单操作上,这显然是正确的:action="index.php?page=agenda"

<form class="filter_form" method="get" action="index.php?page=agenda">
    <div class="location">
        <p class="filter_by">filter by:</p>
        <label for="location">location</label>
        <select name="location" id="location">
            <option value="all">-- Locations --</option>
            <?php foreach($locations as $location): ?>
                <option value="<?php echo $location['location']; ?>"
                    <?php
                        if(!empty($_GET['location'])){
                            if($_GET['location'] == $location['location']){
                                echo ' selected';
                            }
                        }
                    ?>
                  >
                    <?php echo $location['location']; ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>

    <div class="checker_wrapper">
        <div>
            <input type="checkbox" name="check1" id="check1">
            <label for="check1">skills only</label>
        </div>
        <div class="bottom_row">
            <input type="radio" name="group" id="radio1">
            <label for="radio1">day</label>
        </div>
    </div>

    <div class="radio_wrapper">
        <div>
            <input type="checkbox" name="check2" id="check2">
            <label for="check1">in group</label>
        </div>
        <div class="bottom_row">
            <input type="radio" name="group" id="radio2">
            <label for="radio2">evening</label>
        </div>
    </div>
    <input class="button filter_button" type="submit" value="filter" />
</form>

当我按下过滤器按钮时,我在主页中,查询字符串是这样的:index.php?location=all,所以$_GET[location]我的项目有效。它只是没有在字符串中添加正确的页面。

标签: phpmysql

解决方案


您的表单正在覆盖您的操作属性中的查询字符串参数。

不是将查询字符串放在操作中,而是添加一个隐藏字段

<form class="filter_form" method="get" action="index.php">
   <!-- ... the rest of your form code -->
    <input class="button filter_button" type="submit" value="filter" />
    <input type="hidden" name="page" id="page" value="agenda" />
</form>

相关帖子:提交带有查询字符串参数和隐藏参数的 GET 表单消失


推荐阅读