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.
答案 0 :(得分:4)
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; }
答案 1 :(得分:3)
如果您确实希望URL看起来不包含空参数,则可以在将它们从查询字符串中过滤后重定向到同一页面。
// 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;
}
但是,老实说,我不知道如何让它们在那里受到伤害。它似乎是添加了一个不必要的额外步骤,如果您确实需要向页面发送一个空参数值,它可能在将来会出现问题。
答案 2 :(得分:2)
try something like this when submitting form
$("#search-form").submit(function(){
$("input").each(function(index, input){
if($(input).val() == "") {
$(input).remove();
}
});
});