Validate empty fields by post method in php

0

my question is that php does not recognize the empty fields inside $_POST , which contains the arrangement of my form.

I have the following code:

index.php

    <!DOCTYPE html>
<html>
<head>
    <title>Document</title>
</head>
<body>
    <form action="recibe.php" method="post">

    <input type="text" placeholder="Nombre:" name="nombre">
        <br>

        <label for="hombre">Hombre</label>
        <input type="radio" name="sexo" value="hombre" id="hombre">
        <br>

        <label for="mujer">Mujer</label>
        <input type="radio" name="sexo" value="mujer" id="mujer">
        <br>

        <select name="year" id="year">
            <option value="2000">2000</option>
            <option value="2001">2001</option>
            <option value="2002">2002</option>
        </select>
        <br>

        <label for="terminos">Aceptas los Terminos?</label>
        <input type="checkbox" name="terminos" id="terminos" value="ok">
        <br>
        <input type="submit" value="Enviar">


    </form>
</body>
</html>

And valid with:

recibe.php

<?php
print_r($_POST);

if(!$_POST) {
    header("Location: http://localhost/Formularios/");
}

else
{
    $nombre = $_POST["nombre"];
    $sexo = $_POST["sexo"];
    $year = $_POST["year"];
    $Terminos = $_POST["terminos"];

    echo "Hola,".$nombre . " eres:". $sexo;
}

?>

It is assumed that if the form is not sent in full, you should go back to the form until all the data is, but show me this:

    Array ( [nombre] => [year] => 2000 )
Notice: Undefined index: sexo in C:\xampp\htdocs\Formularios\recibe.php on line 11

Notice: Undefined index: terminos in C:\xampp\htdocs\Formularios\recibe.php on line 13
Hola, eres:
    
asked by Pedro Robles 04.01.2019 в 22:57
source

3 answers

4

The code you show has an error in the logic of the program:

  

It is assumed that if the form is not sent in full, it must go back to the   form until all the data is ...

with the following validation:

if (! $_POST) {
    header("Location: http://localhost/Formularios/");
}

Redirection will only occur if $ _ POST is completely empty , meaning that if only one of the fields in the form was entered, even if it was a space blank, it no longer redirects.

So that all the fields have a content, or that otherwise, you return to the form , a possible solution would be to use the following validation:

if (! $_POST
    || trim($_POST['nombre'])   === ''
    || trim($_POST['sexo'])     === ''
    || trim($_POST['year'])     === ''
    || trim($_POST['terminos']) === ''
    ) {
    header("Location: http://localhost/Formularios/");
}
    
answered by 05.01.2019 / 10:20
source
3

The solution to the problem is the following:

<?php
// Si todos los campos se han enviado, entonces, «$post» será «true»,
// de lo contrario será «false»:
$post = (isset($_POST['nombre']) && !empty($_POST['nombre'])) &&
        (isset($_POST['sexo']) && !empty($_POST['sexo'])) &&
        (isset($_POST['year']) && !empty($_POST['year'])) &&
        (isset($_POST['terminos']) && !empty($_POST['terminos']));


// Si $post es true (verdadero), entonces se mostrarán los resultados:
if ( $post ) {
    $nombre = htmlspecialchars($_POST["nombre"]);
    $sexo = htmlspecialchars($_POST["sexo"]);
    $year = htmlspecialchars($_POST["year"]);
    $terminos = htmlspecialchars($_POST["terminos"]);

  // Resultado
  echo "Hola <strong>$nombre</strong>, eres <strong>$sexo</strong>";
}else {
  // Si en cambio, es false (falso), entonces volverá al formulario desde
  // donde se envió la petición:
  header("Location: ./");
}
?>

The reason you are presented with this problem is because $ _POST is a global variable of type associative array and therefore continues to exist while sending requests through the POST method of the form. This results in the conditional not being met.

    
answered by 05.01.2019 в 06:06
0

Do not limit yourself to using if it is different from vacuum (& & empty), You can easily create your own parameters without relying on presets.

For example, empty means empty, in which case if the user enters a blank space, the form will be sent and the code you wrote will also be processed because it does not come "empty".

On the other hand, if you create all the possible rules such as:

If( _Post !=”” And _post !=” “ && _post[0] != “ “
    
answered by 08.01.2019 в 00:37