douyan8772 2019-06-07 13:50
浏览 57
已采纳

PHP表单操作不起作用,并且不更新查询字符串

I'm trying to make a filter using a MySQL database and PHP for a school project, but whenever I press my filter button, it doesn't execute the action so the querystring is not updated.

The reason it's referring to my homepage is because of this block code:

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

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

This is when there is no $GET['page'] passed, and it will just refer to my homepage.

So the problem probably lies with my form action, which is clearly correct: 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>

When I press the filter button, I'm in the homepage and the querystring is like this: index.php?location=all, so the $_GET[location] for my project works. It's just not adding the correct page in the string.

  • 写回答

4条回答 默认 最新

  • doupi4649 2019-06-07 13:55
    关注

    Your form is overriding the query string parameters in your action attribute.

    Instead of putting the query string in the action, add a hidden field

    <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>
    

    Related post: submitting a GET form with query string params and hidden params disappear

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色