Answer the question
In order to leave comments, you need to log in
How not to display empty get in url string?
For example, because of a get request, we get such a link
test.ru/?a=1&b=&v=3&g=&f=1 , how to convert it to
test.ru/?a=1&v=3&f=1. That is, empty variables disappear.
Answer the question
In order to leave comments, you need to log in
if (!empty($_GET)) {
$new_get = array_filter($_GET);
if (count($new_get) < count($_GET)) {
$request_uri = parse_url('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], PHP_URL_PATH);
header('Location: ' . $request_uri . '?' . http_build_query($new_get));
exit;
}
}
As an option, when submitting the form, form the url, or rather get it yourself using js.
Empty variables in get because when submitting the form, all fields in the form form a request, regardless of whether the field is empty or hidden, it will still fall into get.
Just catch the form submission with the same jquery, iterate through all the fields inside the form in a loop and form a request (url) from non-empty fields. And then just redirect to this url (document.location.href = url).
Or as an option, remove all empty fields with js before submitting the form, the effect will be the one you want.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question