Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass current URL on form submit

Tags:

forms

php

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.

like image 927
AlxVallejo Avatar asked Feb 01 '12 19:02

AlxVallejo


1 Answers

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.

like image 98
Jim D Avatar answered Oct 15 '22 06:10

Jim D