Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

biniyam20's avatar

Search query does not persist in button action

View file below

              <form action="/browse?search={{$listing->isbn13}}" method="GET">
                  <input class="btn btn-link btn-success btn-xs" id="reusable_button" type="submit" value="{{$listingButtonName}}" name="">
              </form>

On chrome using inspect element I get

<form action="/browse?search=9780061127922" method="GET">

but when I click on the button it does not take me to that url but rather

http://localhost:8001/browse?  //query parameter is stripped

My relevant web routes are below

Route::get('/browse','ListingsController@index');
Route::get('/browse/{listing}','ListingsController@show');

The thing is if I actually put in the url below into google then the correct webpage does come up

http://localhost:8001/browse?search=9780061127922

My question is then why when I make a button click does my search parameter get stripped, even before the request hits my controller?

0 likes
7 replies
Dalma's avatar

I think that once your url has a ? in it that you have passed a URL plus a query string. If you wanted to access this query string you could do so in your method like:

$keyword = $request->get('search');

So in your example it's url is /browse would be handled by your index method.

Try to reformat your url to be /browse/9780051127922 and this should then be directed to your show method and the numeric is available in your method via a parameter.

biniyam20's avatar

Thank you for your response, @dalma To clarify, what I am saying is that before the request hits my controller the search parameter is completely stripped off so in my code below there is no value with the key search in the request variable. What I would like to figure out is how to make sure the search parameter stays on during the whole life of the request (which it should do naturally right?)?

public function index(Request $request)
  {
      $searchTerm = $request->query('search');

      if ($searchTerm) $result = ListingsController::search($searchTerm);
      else $result = ListingsController::showAll();

      extract($result);

      return view('browse.searchTable', [
                'listings' => $listings,
                'queryMessage' => $queryMessage,
                'rowCount' => $rowCount,
                'searchButtonRedirectTo' => '/browse',
                'searchBarPlaceholderText' => 'Search by Title, Author, or ISBN',
                'listingButtonRedirectTo' => '/browse/',
                'listingButtonName' => 'Rent'
      ]);
  }

As a side note, I am using the show method to actually show the listing that corresponds to that id, but I am specifically asking about search which I am handling through get requests and a search parameter. For example, if I search through my search bar that exact isbn the search parameter persists!

Wait, I think I figured it out. I should be using a hidden input to handle the query portion of the url instead of manually trying to add it! See below how I do it for the search input!

<form method="GET" action="{{$searchButtonRedirectTo}}">
  <div class="form-row align-items-center" id="searchDiv">
    <input autofocus class="form-control form-control-lg" type="text" placeholder="{{$searchBarPlaceholderText}}" id="searchInput" name="search" required>
    <button type="submit" onclick="changeColor(this)"id="searchButton" class="btn btn-sm" style=""><i class="fas fa-search fa-2x"></i></button>
  </div>
</form>
Dalma's avatar

I would try a dd($searchterm) right after you recieve you query to ensure that it has been made available within your method as the first step.

biniyam20's avatar

I modified the code to add a hidden input and stripped the action of the form of any parameters other than the base subfolder. This works now. That is a good suggestion to add the dd($search_term) in my controller to ensure it is available. It wasn't originally available in my case, but it is now because the parameter is being passed in the correct way. Thanks for the help!

              <form action="/browse" method="GET">
                <input type="hidden" value="{{$listing->isbn13}}" name="search">
                  <input class="btn btn-link btn-success btn-xs" id="reusable_button" type="submit" value="{{$listingButtonName}}" name="">
              </form>
Dalma's avatar

I'm glad that you found your solution.

biniyam20's avatar

Thank you @dalma I am glad too! If you are interested, you can actually see this specific feature implemented on the home page of my website. The search button for each popular book is what you helped me figure out. Here's the link: http://68.183.172.140

Cronix's avatar
Cronix
Best Answer
Level 67

Problem is because you're using a form with the get method, and using a query string for the forms action attribute. Forms using the get method use the query strings to submit the data, so it is overwriting the query string of your form action. This is an HTML issue - not laravel.

You should probably be sending search as a hidden form attribute and not as a query string parameter of the forms action. If you actually gave your submit button a name, it (and it's value) would have shown up in the query string instead of http://localhost:8001/browse?, like http://localhost:8001/browse?buttonName=aslkdfjasldfj

1 like

Please or to participate in this conversation.