Formularies in PHP

Asked

Viewed 38 times

0

I have a doubt and I believe it is very simple to solve ,how do I do not load the error "Notice: Undefined index: Cot " ,follows below the code , I know it should be pq there is no value, but anyway.

test

<body>
    <form mehthod="POST" action="">
        <input type="double" name="cot"><br>
        <input type="submit" name="btn" value="ENVIAR">
        <hr>
        <?php
        $cot=$_GET["cot"];
        echo $cot;
        ?>
    </form>
</body>

1 answer

1


Try to check if the variable is "set" before. Follow code below:

<?php
if(isset($_GET["cot"])){
    $cot = $_GET["cot"];
}else{
    $cot = "";
}
echo $cot;
?>

It is also possible to do more 'elegant' using If Ternary, but the way I did it already solves your problem.

I hope I’ve helped.

  • 1

    From PHP 7 there is the null coalescence operator: $cot = $_GET['cot'] ?? ''

  • Cool @Andersoncarloswoss. I didn’t know. I’ll give a read on the subject. Thanks for commenting.

Browser other questions tagged

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