Wednesday, May 1, 2019

isset - Form submission results in a blank page in PHP



I am trying to make a login system and i want to create a conditional statement that checks whether the global variable $_POST['submit-form'] is set.



If the global variable $_POST['submit-form'] is set then i want to echo out the fields of the submitted forms. This works fine..



The problem comes when i want to check whether the global variable $_POST['submit-form'] is empty, i get a blank page when i submit the form with nothing. It is supposed to echo out something like "You have entered nothing, please try again'.




I don't know what is wrong.



This is the code for the form.



  



Name:
E-mail:









..and this is the code for the form handler.






if(isset($_POST['submit-form'])) {


$name = $_POST['name'];
$email = $_POST['email'];


if(($_POST['name'] != "") && ($_POST['email']!= "")) {


echo "This is your name: ".$name."
";
echo "This is your email: ".$email;

// header('refresh=3;url = ../leden/index.php');

}

} else {


echo "You have entered nothing or your username and/or password is incorrect, please try again.";

// header('refresh=3;url = /test-form1.php');

}

?>

Answer



The problem with your code is that checking if it's set isn't enough .. Because it may be set and be empty -- Realistically what you want is to check both isset and whether it's empty IE:




if (isset($_POST['submit-form'] && $_POST['submit-form'] != '' && $_POST['submit-form'] != null)  


If the above if statement fails your value for $_POST['submit-form'] is most likely not being submitted.



UPDATE
Check for blank fields



if ($_POST['name'] != '' && $_POST['email'] != ''){

// Do stuff
}else{
if ($_POST['name'] == ''){
echo "name is empty";
}
if ($_POST['email'] == ''){
echo "email is empty";
}
}


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...