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

mufasaparadox's avatar

How to append query string to new url?

To explain this, I’m gonna try to use an example and maybe it’ll give you a better picture of it hopefully. I’m using pagination on my project and added $variable->appends(get)->links to keep query strings in the next page of pagination. Is there a way to use or mimic this when pressing a form button for submission, which would have action attribute with a url, to add a new view on the url and also append those query strings to it like appends get does on pagination?

0 likes
6 replies
jlrdw's avatar

Is there a way to use or mimic this when pressing a form button for submission

A form is usually posted, normally not a query string there.

But if a get request, build it in the form action method.

lostdreamer_nl's avatar

assuming you have a GET form, and you want the current query parameters to resubmit with submitting your form, the easiest way is by adding those query params to your form as well:

@foreach($_GET as $key => $val)
    <input type="hidden" name="{{ $key }}" value="{{ $val }}">
@endforeach

If you put that within your existing form, it will also add hidden fields for all query parameters the url has.

Just make sure only to use this on a <form method='get'>

jlrdw's avatar

Realistically and just trying to be honest, by the time you are using the laravel framework you should be already well-versed in basic HTTP techniques such as get and post request and those kinds of basics.

Jeffrey has free introductory PHP courses as well. Even if you can't do the videos he has free code you can study on GitHub.

mufasaparadox's avatar

@lostdreamer_nl It is a get form and yes, the query paremeters should be resubmit by a second button on the form which would have the new url so for example:http://unicah.localhost/unicahmanager/evaluaciones?name=&fechainicio=2018-09-24&fechafinal=2018-09-24 would be a search query created by the first button and after a click on the second button it should look like this:http://unicah.localhost/unicahmanager/evaluaciones/downloadPDF?name=&fechainicio=2018-09-24&fechafinal=2018-09-24

Cronix's avatar

You could also use javascript for this. In the onsubmit event of the form, grab the current query string from the url with window.location.search, replace the ? with a &, and append it to the end of the submitted form values. Basically just combine the current query string with the one the form generates.

Please or to participate in this conversation.