Rorschach Rorschach - 7 months ago 27
PHP Question

How to add $_POST['variable'] to URL of next page?

I have a simple landing page with a search bar and submit button.

I want the URL of the next page to read:

mysite.com/after_search.php?search=*the search text here*
.

Here is the code from the landing page / index.php page:

<form class="" action="after_search.php?search="<?php echo $_POST['search_title']?> method="post">
<div class="form-group" id="search_wrapper">
<input type="text" id="search_field" class="form-control color-outline" name="search_title" placeholder="Search">


The issue is that $_POST['search_title'] is not set until the next page loads. So, it does not work because $_POST['search_title'] is not defined on this page.

How do I add the contents of the search bar to the URL of the next page?

EDIT 1

It was suggested that I switch to GET instead of POST.

But on the 2nd page, there are many places that use $_POST['...']

Switching all those to $_GET['...'] has allowed it to work.

Thanks, all

Answer

You need to use $_GET method to get the values from a URL:

<form class="" action="after_search.php" method="get">

To get the value, use:

$_GET['search']

If you are passing the value through the link:

?search=the search text here

That means you are using $_GET, thus you need to use $_GET instead of $_POST.

Comments