Arcadia Posted March 17, 2015 Share Posted March 17, 2015 Hello, I have a little problem with my php code an really hope someone can help me out here. I made a php code that show data when I fill in a form but my problem is it doesn't go further that the first two statements it also does not give a error message when a form field is not filled in:( So my main problem is that the last two ifelse statements do not work and it also does not give a error message when a form field is not filled in. I just started with php so really hope someone can help me out here with this stupid question <?php $Name = $_POST ['Name']; $Place = $_POST ['Place']; $Age = $_POST ['Age']; if (isset($_POST['Submit'])) { //Als men op de Submit knop heeft gedrukt van het formulier... if ($_POST['Name'] == "") { $error = "Name is not filled in.<BR />"; } if ($_POST['Age'] == "") { $error .= "Age is not filled in!<BR />"; } if ($_POST['place '] == "") { $error .= "Place is not filled in!"; } } if ($Age > 18) { echo "Hello $Name"; } elseif ($Age < 18) { echo "Hello little $Name"; } elseif ($Age< 18 && $Place == "New York") { echo "Living in: $Place"; } elseif ($Age > 18 && $Place == "New York") { echo "I love New York!!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/295303-problem-with-ifelse-statements/ Share on other sites More sharing options...
kicken Posted March 17, 2015 Share Posted March 17, 2015 (edited) First off, just incase this isn't some copy/paste issue, you really need to work on your formatting. Proper and consistent formatting will help you avoid bugs by making it easier to follow what the code does. Here is your code cleaned up: <?php $Name = $_POST['Name']; $Place = $_POST['Place']; $Age = $_POST['Age']; if (isset($_POST['Submit'])){ //Als men op de Submit knop heeft gedrukt van het formulier... if ($_POST['Name'] == ""){ $error = "Name is not filled in.<BR />"; } if ($_POST['Age'] == ""){ $error .= "Age is not filled in!<BR />"; } if ($_POST['place '] == ""){ $error .= "Place is not filled in!"; } } if ($Age > 18){ echo "Hello $Name"; } elseif ($Age < 18){ echo "Hello little $Name"; } elseif ($Age < 18 && $Place == "New York"){ echo "Living in: $Place"; } elseif ($Age > 18 && $Place == "New York"){ echo "I love New York!!"; } ?> Now, you have a few issues.You should always use isset() to test if input data exists. Failure to do so will result in E_NOTICE level undefined index errors when it does not exist but you try and read it anyway. This means your first three lines need to check if the key exists before reading it. You can do this easily using the ternary operator along with isset(). Since you re-assign the post keys to other variables at the start of the script, you may as well use those variables in your if conditions. $error is potentially undefined since you define it within the if condition relating to $_POST['name']. If that condition is not met but the others are, you'll get an E_NOTICE error about it being undefined. $_POST['place '] is wrong. You never actually output $error anywhere and you do not prevent your second part of the code from running if an error was found. Your last two elseif statements are impossible because in order for one of them to be true, one of the prior two statements must also be true which means it will execute instead of the later ones. It's unclear what the desired output would be from the code alone, but as a general rule when doing an if/elseif/else chain you should order the conditions from most-specific to least-specific. Here is some corrected code that addresses the points above: <?php $Name = isset($_POST['Name'])?$_POST['Name']:''; $Place = isset($_POST['Place'])?$_POST['Place']:''; $Age = isset($_POST['Age'])?$_POST['Age']:''; $error = ''; if (isset($_POST['Submit'])){ //Als men op de Submit knop heeft gedrukt van het formulier... if ($Name == ""){ $error .= "Name is not filled in.<BR />"; } if ($Age == ""){ $error .= "Age is not filled in!<BR />"; } if ($Place == ""){ $error .= "Place is not filled in!"; } } if ($error){ echo $error; } else { if ($Age < 18 && $Place == "New York"){ echo "Living in: $Place"; } elseif ($Age > 18 && $Place == "New York"){ echo "I love New York!!"; } elseif ($Age > 18){ echo "Hello $Name"; } elseif ($Age < 18){ echo "Hello little $Name"; } } ?> Edited March 17, 2015 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/295303-problem-with-ifelse-statements/#findComment-1508278 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.