Jump to content

Elseif


dudejma

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/240155-elseif/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/240155-elseif/#findComment-1233589
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/240155-elseif/#findComment-1233593
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/240155-elseif/#findComment-1233594
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.