试图检查是否输入了用户名 - PHP

-Checking if the username is entered.

-Basically I am trying to say if the user did not enter a username, echo "Please insert a username".

    <form action="form_3.php" method="get">
        Username: <input type="text" name="username" value="" /><br />

        <input type="submit" name="submit" value="Submit" />
    </form>


<?php

$username=$_GET["username"];

if (!isset($username)) {echo "Please insert a USERNAME";}
else{echo "Hello: ".$username;}

 ?>

You can do this simply with HTML.

Username: <input type="text" name="username" value="" required>

You should add your php code on a different page to avoid any load errors.

You shouldn't use

isset();

but

empty();

The "empty" function is internally doing a "isset" on the value and checking that the variable is not an empty string.

http://php.net/manual/en/function.empty.php

The html5 validator can also be used to avoid your serveur to actually have to return the form saying "Please enter your username", but it can be easily disabled by a visitor.

<input type="text" name="username" value="" required>

When I load the page the first time it shows an error because the username is already empty. How can fix that error?