How to suspend the $_GET parameter if the array is empty?

Asked

Viewed 1,338 times

1

This parameter tipo[] is sent to the url even if it is empty. There is a way not to send this parameter if no value has been selected?

<input type="checkbox" name="tipo[]" value="APARTAMENTO/APTO DUPLEX" id="tp1">
<label for="tp1">Apartamento</label>

<input type="checkbox" name="tipo[]" value="CASA" id="tp2">
<label for="tp2">Casa</label>

<input type="checkbox" name="tipo[]" value="CASA EM CONDOMINIO" id="tp3">
<label for="tp3">Casa Condomínio</label>

3 answers

3

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

2

You can block the form submission if the value is empty. Can be via javascript or by HTML5 by tag 'required'

Otherwise, you have to perform the verification of these values in the backend.

In the case of PHP you can mount a simple check of $_GET as follows:

if(!empty($_GET["tipo"])){
    $tipo = $_GET["tipo"];
}

Follow documentation for consultation.

http://php.net/manual/en/reserved.variables.get.php

  • 2

    Explain to him how to do.

  • 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.

  • 2

    Marcos, I supplemented the answer with an example code to validate $_GET. !

  • Raphael, what would be procedure with selects?

  • @We would love to use the array_key_exists instead of isset?

  • Raphael, that if($_GET['var']) will generate a notice in PHP, no?... I believe the answer was marked as correct because of array_key_exists...

  • @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.

  • 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.

  • @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

  • 1

    @We see the difference between isset and array_key_exists is that the isset checks only the contents of the key. If the key has a value null, the isset returns false. Already the array_key_exists checks the key, regardless of the value. In your example I recommend the isset, because what matters is the value of the key, not if it exists.

  • @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().

Show 6 more comments

1

Do not send: The correct would be to use javascript for not send to the server.

However your server will anyway be waiting for a value, even if it is 'nothing' (null), and when sending null, no longer need javascript to cancel;

Ideally, you should check in PHP if the value was set or not.

<?php

if(isset($_GET['tipo']))
{
    echo "Recebi algum valor na variável TIPO via GET.<br>";

    $tipos = $_GET['tipo'];
    foreach ($tipos as $v) 
    {
        echo "$v<br>";
    }
}
else
{
    echo "Não recebi nada na variável TIPO via GET.<br>";    
}

?>
  • 1

    isset will not work in this case. Because $_GET['type'] comes as empty string. E isset() will return TRUE.

  • 1

    I just tested to check, it is null yes. The variable is not sent via GET. att

Browser other questions tagged

You are not signed in. Login or sign up in order to post.