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

1

This tipo[] parameter is sent to the url even though it is empty. Is there a way to not send this parameter if no value was 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>
    
asked by anonymous 29.10.2014 / 15:20

3 answers

2

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

Otherwise, you have to check these values on the backend.

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

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

Follow the documentation for consultation.

link

    
29.10.2014 / 15:24
2

The page code is on the client side, ie it is in the browser rather than the server.

So, via PHP you have no way to control what will or will not go to the URL based on your fill or not.

If you want to exclude from the URL certain parameters that are in turn fields of a form submitted with the GET method, 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 default, disabled elements are not submitted by submitting the form.

This gives us effective control over what will or will not be shipped. In the example above we are selecting all empty elements and deactivating them so that they are not sent when submitting the form.

Your case

In your case, it looks like you want to delete% with% 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);
});
    
29.10.2014 / 20:09
1

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

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

Ideally you check in PHP whether the value has been 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>";    
}

?>
    
29.10.2014 / 17:34