在表单提交时传递当前网址 [英] Pass current URL on form submit

查看:136
本文介绍了在表单提交时传递当前网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为将值添加到查询字符串的延续一个URL ,我有一个搜索框,将添加额外的参数到当前的URL。

  http:// example.com/?s=original+new+arguments&fq=category 

表单代码看起来像这:

 < form id =FilterSearchmethod =getaction =http://example.com/ search_result.php> 

其中search_result.php只是我创建的一个新页面,用于解析使用新URL的提交和重定向。

如何将原始网址传递给search_result.php以便我可以操纵网址?



编辑#1 :我添加了一个隐藏的输入:

 <?php $ current_url = $ _SERVER [HTTP_HOST]。 $ _SERVER [ REQUEST_URI]; ?> 
< input type =hiddenname =current_urlvalue =<?php $ current_url;?> />
< input name =sendid =sendtype =submitvalue =Search/>

我的输出仅将参数添加到提交页面(search_result.php),而不是预期的查询<$ p
$ b

 <?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 =hiddenname =current_urlvalue =(当前网址)/> 

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






编辑回复编辑:

您可以 echo $ location;在这行后面:

  $ location = $ _GET ['current_url']; 

让我知道输出是什么?这会告诉我们它是否被错误地传递或处理不正确。


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.

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.

How can I pass the original URL to search_result.php so that I can then manipulate the URL?

EDIT #1 : I added a hidden input:

<?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.

<?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:

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

EDIT #3 I echoed the output URL and it's correct. I got rid of the preg_match just to see if I could get the same URL when I processed the form. Interestingly, I got a page not found. I decided to add http:// the header and it worked:

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

So therefore, I believe that the only thing wrong here is the 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.


EDIT to respond to edits:

Could you 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.

这篇关于在表单提交时传递当前网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆