Reputation: 81
Hello stackoverflow community, I need help with GET
stuff. So I've got advanced search, and when I submit form, browser shows this link:
/page_korteles.php?kriterijus-susirgo-nuo=&kriterijus-susirgo-iki=&kriterijus-gimimo-nuo=&kriterijus-gimimo-iki=&kriterijus-isdavimo-nuo=&kriterijus-isdavimo-iki=&kriterijus-ivedimo-nuo=&kriterijus-ivedimo-iki=&kriterijus-isplestinis=ne
Is it possible to clear empty parameters? Or maybe there is different way to work with this? Really need some ideas.
Upvotes: 1
Views: 1929
Reputation: 41820
If you actually want the URL to look not include the empty parameters, you can redirect to the same page after filtering them out of the query string.
// using a callback in array_filter can avoid filtering non-empty falsey values (0, etc.)
$not_blank = array_filter($_GET, function($x) { return $x != ''; });
if ($not_blank != $_GET) {
$query = http_build_query($not_blank);
header('Location: ' . $_SERVER['PHP_SELF'] . "?$query") ;
exit;
}
Honestly, though, I don't see how it hurts anything to have them there. It seems to be adding an unnecessary extra step and it may cause problems in the future if you actually need to send an empty parameter value to the page.
Upvotes: 3
Reputation: 72269
You can simply use array_filter()
:-
print_r(array_filter($_GET));
More knowledge:- https://secure.php.net/manual/en/function.array-filter.php
Note:- The above function will not remove anything from your url, just remove empty value elements from the array variable $_GET
.
Maybe you want something like below (this suggestion given by another persone, i am just adding it because may be it helpful for you):-
if(array_filter($_GET) == $_GET) { //stays on page and do other stuff (already removed empty) }else{ // remove empty keys and redirect to url without them; }
Upvotes: 4
Reputation: 2108
try something like this when submitting form
$("#search-form").submit(function(){
$("input").each(function(index, input){
if($(input).val() == "") {
$(input).remove();
}
});
});
Upvotes: 2