if(isset($_POST['submit'])) {
if(!isset($_POST['userName'])) {
$username = 'Anonymous';
}
else $username = $_POST['userName'];
}
I cannot get the $username to be “Anonymous”? It is either blank or the value of $_POST['userName'].
Advertisement
Answer
isset() will return true if the variable has been initialised. If you have a form field with its name value set to userName, when that form is submitted the value will always be “set”, although there may not be any data in it.
Instead, trim() the string and test its length
if("" == trim($_POST['userName'])){
$username = 'Anonymous';
}