将表单值附加到已包含变量的 URL
我有一个页面显示用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你为什么不把它们放在隐藏的形式中呢?
why don't you put them in the form as hidden?
您可以为每个要传输的变量创建一个隐藏字段,以便它们也被附加。
更好的是,采用
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.将其他变量输出为
而不是表单操作,这样它们就会被纳入您的查询字符串中。
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.使用此 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.