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

ajitdas's avatar

How can i submit form keeping existing query string intact in url in laravel

Hi, I have a URL something like this : foo?country=usa&state=ny and in foo I have a form which gives me values of firstname & lastname and few more with a submit button.

But when i click submit it gives foo?firstname=john&lastname=doe And the other query i.e country and state get disappear or in other word i am losing them.

Can anyone please tell me how can i peserve them even when i am submiting a form ?

Please note: I can not use any hidden field because there are many forms in foo blade as well as as there can be many query that can come to foo blade not just country and state, there can be age, zipcode etc.

My form in foo.blade.php is like this

<form method="get" action ="{{Request::fullUrl()}}">
   <div class="form-group">
      <input type="text" class="form-control" name="firstname" placeholder="first name">
    </div>
  
    <button type="submit" class="btn btn-green-small">Apply</button>
</form>

    //Another form
 <form method="get" action ="{{Request::fullUrl()}}">
     
       <div class="form-group">
          <input type="text" class="form-control" name="lastname" placeholder="first name">
           </div>
        <button type="submit" class="btn btn-green-small">Apply</button>
    </form>
0 likes
7 replies
click's avatar
click
Best Answer
Level 35

That is not possible. See also this topic on stackoverflow which gives some explanation: https://stackoverflow.com/questions/1116019/submitting-a-get-form-with-query-string-params-and-hidden-params-disappear

The normal approach would place those fields as hidden input fields. But you say you can't/don't want to do that.

Do you really need to submit your form with the use of 'GET' can't you use 'POST' instead? That could solve your problem also.

There are several other solutions like listening for the submit event of your form and creating the get url yourself (with javascript).

1 like
Snapey's avatar

Use POST

merge your parameters with the route so that you post to the controller with the additional parameters

After saving the form data, redirect, again including the additional parameters.

Alternatively persist then in session

3 likes
jlrdw's avatar

You could use jquery ajax, could still use GET (if needed) and the querystring wouldn't show. But others are correct, a POST would be better.

And I am so sorry If I totally misunderstood the question. I probably did, but another person will probably come along with a better answer than mine.

ajitdas's avatar

I think i will go with @click asnwer :D I added this jquery script to manually add the query

       ` $('.filterForm').on('submit',function(e){
            e.preventDefault();
            var formData=$(this).serialize();
            var fullUrl = window.location.href;
            var finalUrl = fullUrl+"&"+formData;
            window.location.href = finalUrl;

        })` 
Snapey's avatar

I added this jquery script to manually add the query

Why? you can add them in blade view?

ajitdas's avatar

@Snapey no not that, i can not use post because i need to display the filters in the url. And route will be difficult i guess because some filter can have many value. for example country it can have many values like usa,uk etc in one single search filter that is why

Snapey's avatar

I did not say remove them from the URL

If you have these in the URL then they came from the controller in the previous request when you showed the form

If, in the controller, you pass these to the view as for instance $state and $country, then the form's action can have the $state and $country added to the url as querystring parameters.

Please or to participate in this conversation.