How to avoid showing error when using the $ _REQUEST method in php?

2

Well I explain, when loading my page.php from a local server it shows me the following error: Undefined index: var_entrada ... line 17.This is the code:

 <form action="pagina.php">
    <p><input type="text" name="var_entrada" ></p>
    <input type="submit" value="enviar">
 </form>

 <?php 
        $var1 = 0;
        $cont = 0;
        $suma = 0;

        $var1 = $_REQUEST['var_entrada'];

        while($cont <= $var1){
            $suma += $cont;
            $cont ++;
        }
        echo "Numero de entrada: ".$var1."<br>";
        echo "Suma: ".$suma;
    ?>
    
asked by Renzo_vzn 01.02.2018 в 23:22
source

1 answer

1

With isset it is validated that this data or variable exists, in this case the data that was sent in the request:

if (isset($_REQUEST['var_entrada'])) {
    $var1 = 0;
    $cont = 0;
    $suma = 0;

    $var1 = $_REQUEST['var_entrada'];

    while($cont <= $var1){
        $suma += $cont;
        $cont ++;
    }
    echo "Numero de entrada: ".$var1."<br>";
    echo "Suma: ".$suma;
}
    
answered by 01.02.2018 / 23:30
source