简体   繁体   中英

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.

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; 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 . 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. ie

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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