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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助