[英]Pass current URL on form submit
As a continuation of Adding Values to a Query String in a URL , I have a search box that will add additional arguments to the current URL. 作为向URL中的查询字符串添加值的延续,我有一个搜索框,它将向当前URL添加其他参数。
http://example.com/?s=original+new+arguments&fq=category
The form code looks like this: 表单代码如下所示:
<form id="FilterSearch" method="get" action="http://example.com/search_result.php">
Where search_result.php is just a new page I created to parse the submit and redirect using the new URL. 其中search_result.php只是我创建的一个新页面,用于使用新URL解析提交和重定向。
How can I pass the original URL to search_result.php so that I can then manipulate the URL? 如何将原始URL传递给search_result.php,以便我可以操作URL?
EDIT #1 : I added a hidden input: 编辑#1 :我添加了一个隐藏的输入:
<?php $current_url = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; ?>
<input type="hidden" name="current_url" value="<?php $current_url; ?>" />
<input name="send" id="send" type="submit" value="Search" />
My output is only adding the arguments to the submission page (search_result.php) and not the intended query page. 我的输出只是将参数添加到提交页面(search_result.php)而不是预期的查询页面。
<?php
if (isset($_GET['send'])){
$searchField = urlencode($_GET['SearchField']);
$location = $_GET['current_url'];
$location = preg_replace($location, '/([?&]s=)([^&]+)/', '$1$2+'.$searchField);
header('Location: '.$location);
}
?>
I'm either passing them wrong or getting them wrong or both! 我要么把它们弄错了,要么弄错了,要么两者都错了!
EDIT #2 : Output URL is like: 编辑#2 :输出网址如下:
http://example.com/wp-content/themes/child_theme/search_result.php?SearchField=newTerm¤t_url=www.example.com%2F%3Fs%3DoldTerm%26searchsubmit%3D&send=Search
EDIT #3 I echoed the output URL and it's correct. 编辑#3我回显了输出网址,这是正确的。 I got rid of the preg_match just to see if I could get the same URL when I processed the form.
我摆脱了preg_match只是为了看看我在处理表单时是否可以获得相同的URL。 Interestingly, I got a page not found.
有趣的是,我找不到一个页面。 I decided to add http:// the header and it worked:
我决定添加http://标题并且它有效:
header('Location: http://'.$location);
So therefore, I believe that the only thing wrong here is the preg_replace. 因此,我认为这里唯一不对的是preg_replace。
I think you're looking for the hidden input. 我想你正在寻找隐藏的输入。
Something like this: 像这样的东西:
<input type="hidden" name="current_url" value="(Current URL here)" />
Then access it just like you would any other $_GET or $_POST parameter. 然后像访问任何其他$ _GET或$ _POST参数一样访问它。
EDIT to respond to edits: 编辑以回复编辑:
Could you echo $location; die;
你能
echo $location; die;
吗echo $location; die;
echo $location; die;
after this line: 在这一行之后:
$location = $_GET['current_url'];
and let me know what the output is? 让我知道输出是什么? This will tell us whether it's being passed incorrectly or processed incorrectly.
这将告诉我们是错误传递还是错误处理。
Just use $_SERVER['HTTP_REFERER']
in your search_result.php
. 只需在
search_result.php
使用$_SERVER['HTTP_REFERER']
。 Another option is to use hidden input, but I would go with the first one. 另一个选择是使用隐藏输入,但我会选择第一个。
The better way to get current URl you create a hidden input and in that you pass current URL and catch in server end. 获取当前URl的更好方法是创建隐藏输入,并在其中传递当前URL并捕获服务器端。 ie
即
<input type="hidden" name="cur_url" value="(your Current URL here)" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.