繁体   English   中英

在表单提交上传递当前URL

[英]Pass current URL on form submit

作为向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 :输出网址如下:

http://example.com/wp-content/themes/child_theme/search_result.php?SearchField=newTerm&current_url=www.example.com%2F%3Fs%3DoldTerm%26searchsubmit%3D&send=Search

编辑#3我回显了输出网址,这是正确的。 我摆脱了preg_match只是为了看看我在处理表单时是否可以获得相同的URL。 有趣的是,我找不到一个页面。 我决定添加http://标题并且它有效:

header('Location: http://'.$location);

因此,我认为这里唯一不对的是preg_replace。

我想你正在寻找隐藏的输入。

像这样的东西:

<input type="hidden" name="current_url" value="(Current URL here)" />

然后像访问任何其他$ _GET或$ _POST参数一样访问它。


编辑以回复编辑:

你能echo $location; die;echo $location; die; echo $location; die; 在这一行之后:

$location =  $_GET['current_url'];

让我知道输出是什么? 这将告诉我们是错误传递还是错误处理。

只需在search_result.php使用$_SERVER['HTTP_REFERER'] 另一个选择是使用隐藏输入,但我会选择第一个。

获取当前URl的更好方法是创建隐藏输入,并在其中传递当前URL并捕获服务器端。

<input type="hidden" name="cur_url" value="(your Current URL here)" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM