U
U
Uzair Izha2015-11-12 17:54:59
PHP
Uzair Izha, 2015-11-12 17:54:59

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

2 answer(s)
M
Mikhail Petrov, 2017-08-04
@mihacoder

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;
            }
        }

Insert somewhere at the very beginning of the page or entry point, it will clean up any empty GET parameters, and there is no need to be smart in every form.

A
Andrew, 2015-11-12
@allard

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 question

Ask a Question

731 491 924 answers to any question