在表单提交上传递当前URL

Alx*_*ejo 5 php forms

作为向URL中的查询字符串添加值的延续,我有一个搜索框,它将向当前URL添加其他参数.

http://example.com/?s=original+new+arguments&fq=category
Run Code Online (Sandbox Code Playgroud)

表单代码如下所示:

<form id="FilterSearch" method="get" action="http://example.com/search_result.php">
Run Code Online (Sandbox Code Playgroud)

其中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" />
Run Code Online (Sandbox Code Playgroud)

我的输出只是将参数添加到提交页面(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);
                }
?>
Run Code Online (Sandbox Code Playgroud)

我要么把它们弄错了,要么弄错了,要么两者都错了!

编辑#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
Run Code Online (Sandbox Code Playgroud)

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

header('Location: http://'.$location);
Run Code Online (Sandbox Code Playgroud)

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

Jim*_*m D 9

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

像这样的东西:

<input type="hidden" name="current_url" value="(Current URL here)" />
Run Code Online (Sandbox Code Playgroud)

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


编辑以回复编辑:

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

$location =  $_GET['current_url'];
Run Code Online (Sandbox Code Playgroud)

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