The page code is on the client side, that is, it is in the browser and not on the server.
So, via PHP you have no way to control what goes or does not go to the URL based on whether it is filled in or not.
If you want to exclude from the URL certain parameters that in turn are fields of a form submitted with the method GET, you will have to make use of Javascript.
jQuery
// Anexar código à submissão do formulário
$('#idDoMeuFormulario').submit(function(){
// desativar elementos sem valor
$('#idDoMeuFormulario :input[value=""]').attr('disabled', true);
});
By the standards, the elements disabled
are not sent with the form submission.
This gives us effective control over what will or will not be sent. In the example above we are selecting all empty elements and disabling them so that they are not sent when submitting the form.
Your case
In your case, it seems you intend to exclude checkbox
unmarked, so you can:
// Anexar código à submissão do formulário
$('#idDoMeuFormulario').submit(function(){
// desativar caixas de seleção não marcadas
$('#idDoMeuFormulario input:checkbox:not(:checked)').attr('disabled', true);
});
Explain to him how to do.
– Maniero
I cannot block the form because it sends several parameters and one of them will always return a value. I have a
URL
giant with parameters without values, I would like to avoid this.– Marcos Vinicius
Marcos, I supplemented the answer with an example code to validate $_GET. !
– Raphael Ramos
Raphael, what would be procedure with
selects
?– Marcos Vinicius
@We would love to use the
array_key_exists
instead ofisset
?– gmsantos
Raphael, that
if($_GET['var'])
will generate anotice
in PHP, no?... I believe the answer was marked as correct because ofarray_key_exists
...– brasofilo
@gmsantos, had never seen it either, but it seems a valid alternative... I believe I have seen some variant with
in_array
+array_key_exists
.– brasofilo
Actually @Marcosvinicius, both will not work. Because within the $_GET variable the 'type' key is set. Both isset() and the array_key_exists verify the existence of the key. In this case, we will have to use the Empty() function. I changed the example.
– Raphael Ramos
@gmsantos using only if($_GET['var']) also works. PHP converts the values to boleanos. However, it considers an empty string as a True value. Unlike when you have an int = 0. The if of an empty integer is considered false by PHP. As well as variables with the NULL value
– Raphael Ramos
@We see the difference between
isset
andarray_key_exists
is that theisset
checks only the contents of the key. If the key has a valuenull
, theisset
returns false. Already thearray_key_exists
checks the key, regardless of the value. In your example I recommend theisset
, because what matters is the value of the key, not if it exists.– gmsantos
@gmsantos. exactly! The problem is that the boy is validating a GET. And anyway there will come the $_GET['type']. In case the user does not fill this field it will come as empty string. What for PHP is different from NULL. isset considers an empty string to be true. That’s why I recommend using Empty() instead of isset().
– Raphael Ramos