作为Adding Values to a Query String in a URL的延续,我有一个搜索框,它将向当前的URL添加额外的参数。
http://example.com/?s=original+new+arguments&fq=category表单代码如下所示:
<form id="FilterSearch" method="get" action="http://example.com/search_result.php">其中,search_result.php只是我为解析提交和使用新URL重定向而创建的一个新页面。
如何将原始URL传递给search_result.php,以便操作该URL?
编辑#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" />我的输出只是将参数添加到提交页(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);
}
?>我不是把他们错了就是把他们搞错了,或者两者都错!
编辑#2:Output 如下所示:
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编辑#3我呼应了输出,它是正确的。我去掉了preg_match,只是为了看看在处理表单时是否可以得到相同的URL。有趣的是,我找到了一页。我决定将http://添加到头中,它起了作用:
header('Location: http://'.$location);因此,我认为这里唯一的错误是preg_replace。
发布于 2012-02-01 19:07:22
我想你在找隐藏的信息。
就像这样:
<input type="hidden" name="current_url" value="(Current URL here)" />然后访问它,就像访问任何其他$_GET或$_POST参数一样。
编辑以响应编辑:
请您在这行之后echo $location; die;:
$location = $_GET['current_url'];让我知道输出是什么?这将告诉我们它是被错误地传递了还是被错误地处理了。
发布于 2012-02-01 19:40:59
只需在$_SERVER['HTTP_REFERER']中使用search_result.php即可。另一种选择是使用隐藏输入,但我会选择第一个。
发布于 2017-07-19 06:00:51
获得当前URl的更好方法是创建一个隐藏输入,并在服务器端传递当前URL和catch。即
<input type="hidden" name="cur_url" value="(your Current URL here)" />https://stackoverflow.com/questions/9101592
复制相似问题