SilentQ-noob- Posted July 19, 2007 Share Posted July 19, 2007 I've made a simple form, and want to make an error message for fields not being filled out - this is what I have $len = 1; $name = $_POST['name']; $lastname = $_POST['lastname']; $email = $_POST['email']; print "<br />"; if(strlen($name) < $len) { echo "The Name field has not been filled out"; print "<br />"; if(strlen($lastname) < $len) echo "The Last Name field has not been filled out"; print "<br />"; if(strlen($email) < $len) echo "The E-mail field has not been filled out"; print "<br />"; } else { echo " thanks for filling out our form" ; My problem is, is that I dont know how to make the else part not appear. For example, if I fill in only one field, it tells me that the others need to be filled out, but also states "thanks for filling out our form". Any Help/Advice appreciated. Link to comment https://forums.phpfreaks.com/topic/60841-solved-simple-form-if-statement-help/ Share on other sites More sharing options...
thefortrees Posted July 19, 2007 Share Posted July 19, 2007 Your problem lies with the braces. Each if statement should have separate braces - the way you have it, the last name and email error messages will only print if the name field has not been filled out. Instead of what you have, you need this: if(strlen($name) < $len) { echo "The Name field has not been filled out"; print "<br />"; } if(strlen($lastname) < $len) { echo "The Last Name field has not been filled out"; print "<br />"; } if(strlen($email) < $len) { echo "The E-mail field has not been filled out"; print "<br />"; } Also, There are a few things you can do to improve this script. 1) Use the empty() function to see if a string is empty. Read up on it as well. http://us.php.net/empty if (empty($name)) //Do whatever 2) Create a variable that will receive the desired strings and concatenate each error to the variable. Then check to see if the variable is empty. If it is, then there are no errors and you can echo "Thanks for filling out the form. $errors = ""; if (empty($name)) $errors .= "The name field has not been filled out <br>"; if (empty($lastname)) $errors .= "The last name field has not been filled out <br>"; if (empty($email)) $errors .= "The email field has not been filled out <br>"; if (empty($errors)) echo "Thanks!" else echo $errors; Link to comment https://forums.phpfreaks.com/topic/60841-solved-simple-form-if-statement-help/#findComment-302704 Share on other sites More sharing options...
jvrothjr Posted July 19, 2007 Share Posted July 19, 2007 This way it has to past each step to get to the next check. $len = 1; $name = $_POST['name']; $lastname = $_POST['lastname']; $email = $_POST['email']; print "<br />"; if(empty($name)) {echo "The Name field has not been filled out<br>";} else { if(empty($lastname)) {echo "The Last Name field has not been filled out<br>";} else { if(empty($email)) {echo "The E-mail field has not been filled out<br>";} else { echo " thanks for filling out our form" ; } } } [\code] Link to comment https://forums.phpfreaks.com/topic/60841-solved-simple-form-if-statement-help/#findComment-302710 Share on other sites More sharing options...
SilentQ-noob- Posted July 19, 2007 Author Share Posted July 19, 2007 Thanks to both of you for replying, but I think I'll go with what thefortrees showed me just because it is a bit cleaner and more straight forward. Appreciate it. -noob- Link to comment https://forums.phpfreaks.com/topic/60841-solved-simple-form-if-statement-help/#findComment-302717 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.