dudejma Posted June 22, 2011 Share Posted June 22, 2011 I'm sure this is a stupid question, but I am new to PHP. I was wondering how many else ifs you can have. I'm trying to make a form and I want it to die if something is empty or not filled in right, but I'm not sure how to check all of the errors then die. Would the elseif statement not even be good for checking multiple errors? I have more than just three to check. Quote Link to comment https://forums.phpfreaks.com/topic/240155-elseif/ Share on other sites More sharing options...
requinix Posted June 22, 2011 Share Posted June 22, 2011 You can have as many else/ifs as you want, to give a direct answer. But depending on a few things there can be smarter ways of accomplishing the same tasks - especially if you find yourself writing the same type of code in each block (such as "if $x is empty then display message $y"). What code do you have so far? Quote Link to comment https://forums.phpfreaks.com/topic/240155-elseif/#findComment-1233589 Share on other sites More sharing options...
dudejma Posted June 22, 2011 Author Share Posted June 22, 2011 if (empty($fname)) { echo "The first name field was left blank. Please correct entry. <br />"; } if (empty($lname)) { echo "The last name field was left blank. Please correct entry. <br />"; } if (empty($email)) { echo "The email field was left blank. Please correct entry. <br />"; } if ($country == "NONE") { echo "The country field was not selected. Please correct entry. <br />"; } if (empty($vatsim)) { echo "The VATSIM ID was left blank. Please correct entry. <br />"; } if ($birthMonth == 0) { echo "The birth month field was not selected. Please correct entry. <br />"; } if ($birthDay == 0) { echo "The birth day field was not selected. Please correct entry. <br />"; } if ($birthYear == 0) { echo "The birth year field was not selected. Please correct entry. <br />"; } if ($hub == 0) { echo "The hub field was not selected. Please correct entry. <br />"; } if ($agree != true) { echo "You do not agree to the terms of use. Please correct entry. <br />"; } That's what I have, but I realized that it's not going to die and if I make each one die, then it won't test each one at the same time. What would you suggest? Quote Link to comment https://forums.phpfreaks.com/topic/240155-elseif/#findComment-1233593 Share on other sites More sharing options...
Pikachu2000 Posted June 22, 2011 Share Posted June 22, 2011 Having form validation set up to simply die with an error message is about the fastest way to get people to leave and not come back. The better way to do it would be to validate everything at once, store any errors in an array, and present the errors along with the form already filled in with the information the user entered so they can simple correct the mistakes and continue. [utl=http://www.phpfreaks.com/forums/index.php?topic=335390.msg1579961#msg1579961]THIS POST[/url] has a basic form with such validation. Paste it in and play with it so you can see how it works. Quote Link to comment https://forums.phpfreaks.com/topic/240155-elseif/#findComment-1233594 Share on other sites More sharing options...
dudejma Posted June 22, 2011 Author Share Posted June 22, 2011 Thanks! I have spent a lot of time trying to find something like this. Guess I didn't look hard enough. Quote Link to comment https://forums.phpfreaks.com/topic/240155-elseif/#findComment-1233599 Share on other sites More sharing options...
dudejma Posted June 23, 2011 Author Share Posted June 23, 2011 Will that code only work if the form is on the same page as the action? Quote Link to comment https://forums.phpfreaks.com/topic/240155-elseif/#findComment-1233624 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.