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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?