将表单值附加到已包含变量的 URL

发布于 2024-12-12 01:23:14 字数 960 浏览 0 评论 0原文

我有一个页面显示用 PHP 编写的 MYSQL 查询的结果。 URL 包含用户在上一页提交的变量,如下所示:

www.mydomain.com/search/?var1=xx&var2=xx&var3=xx

当用户位于结果页面时,他们需要能够进行排序结果。为此,我有一个 SELECT 表单

<form action="/search<?php echo $urlQuery; ?>"  name="order "class="formsrch" method="post" >
            <label>sort by:</label>
            <select class="order" id="order" name="order" onChange="this.form.submit()">
                <option value="pricedesc">Price High to Low</option>
                <option value="priceasc">Price Low to High</option>
                <option value="dist">Distance</option>
            </select>
        </form>

变量 $urlQuery 包含要附加到 url 上的字符串: 即 $urlQuery = "?var1=xx&var2=xx&var3=xx"

问题是,提交表单时,页面会重新加载,并且 url 末尾是 ?order=dist。

有没有办法用 & 符号替换问号,以便加载页面并检索 order 的值?

或者,如果有人有更好的方法来完成整个事情,我绝对愿意接受建议。 谢谢

I've got a page showing the results of a MYSQL query written in PHP. The URL has the variables that the user submitted on the previous page as:

www.mydomain.com/search/?var1=xx&var2=xx&var3=xx

When the user is on the results page they need to be able to sort the results. To do this I've got a SELECT form

<form action="/search<?php echo $urlQuery; ?>"  name="order "class="formsrch" method="post" >
            <label>sort by:</label>
            <select class="order" id="order" name="order" onChange="this.form.submit()">
                <option value="pricedesc">Price High to Low</option>
                <option value="priceasc">Price Low to High</option>
                <option value="dist">Distance</option>
            </select>
        </form>

The variable $urlQuery contains the string to be appended onto the url:
i.e. $urlQuery = "?var1=xx&var2=xx&var3=xx"

The problem is that when the form is submitted the page is reloaded and at the end of the url is ?order=dist.

Is there a way of replacing the question mark with an ampersand so the page will load and the value of order can be retreived?

Or, if anyone has a better way of doing the whole thing I'm definitely open to suggestions.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼角的笑意。 2024-12-19 01:23:14

你为什么不把它们放在隐藏的形式中呢?

<?php
$extraVars = "";
  foreach($_GET AS $key=>$value) {
   $extraVars .= '<input type="hidden" name="'.$key.'" value="'.$value.'" />';
  }
?>
<form action="/search"  name="order "class="formsrch" method="post" >
    <?php echo $extraVars;?>
    <label>sort by:</label>
    <select class="order" id="order" name="order" onChange="this.form.submit()">
        <option value="pricedesc">Price High to Low</option>
        <option value="priceasc">Price Low to High</option>
        <option value="dist">Distance</option>
    </select>
</form>

why don't you put them in the form as hidden?

<?php
$extraVars = "";
  foreach($_GET AS $key=>$value) {
   $extraVars .= '<input type="hidden" name="'.$key.'" value="'.$value.'" />';
  }
?>
<form action="/search"  name="order "class="formsrch" method="post" >
    <?php echo $extraVars;?>
    <label>sort by:</label>
    <select class="order" id="order" name="order" onChange="this.form.submit()">
        <option value="pricedesc">Price High to Low</option>
        <option value="priceasc">Price Low to High</option>
        <option value="dist">Distance</option>
    </select>
</form>
空城之時有危險 2024-12-19 01:23:14

您可以为每个要传输的变量创建一个隐藏字段,以便它们也被附加。

更好的是,采用 get 形式的方法,然后使用 $_REQUEST 在后端脚本中是全局的。

You could make a hidden field for each of the variables you want to transfer so that they get appended too.

Better still, make the method of the form get and then access all the variables with the $_REQUEST global in the backend script.

情话已封尘 2024-12-19 01:23:14

将其他变量输出为 而不是表单操作,这样它们就会被纳入您的查询字符串中。

Output the other variables as <input type="hidden" name="var1" value="xx" /> instead of as the form action, so they'll get taken into your query string.

む无字情书 2024-12-19 01:23:14

使用此 action="/search" 和 $urlQuery = "?var1=xx&var2=xx&var3=xx" ,

然后要将值附加到查询字符串上,您应该将表单方法更改为“GET”。

如果您想将表单方法保留为“POST”,那么在提交表单时将需要一些 JavaScript 来更改表单的操作。

With this action="/search" and $urlQuery = "?var1=xx&var2=xx&var3=xx"

then to have the value appended on the querysting you should change the form method to "GET".

If you want to keep the form method to "POST" then some javascript would be required to change the action of the form when the form is submitted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文