Jump to content

multiple if statement


jimmyt1988

Recommended Posts

Hi all,

 

I basically want to check if every single one of the forms fields is filled out. If not return an error...

The below works.. However, I cannot give out individual response ie. you still need to fill in your firstname and email address.

Is it a switch statement I'm looking for, and can I have a good example of how I can implement it into my problem?

 

<?php
    if ($emailAddressValidation>0){
        echo "Email address already taken";
    }
    else if ($usernameValidation>0){
        echo "Username already taken";
    }
    else if ($firstName = '' && $lastName = '' && $day = '' && $month = '' && $year = '' && $country = '' && $emailAddress = '' && $username = '' && $password = ''){    
        mysql_query("insert into userInformation(firstName, lastName, day, month, year, country, emailAddress, username, password) 
            values('$firstName','$lastName','$day','$month','$year','$country','$emailAddress','$username','$convertedPassword')");
    }
    else{
        echo "You have missed out one or more of the following required fields";
    }
?>

 

 

Link to comment
Share on other sites

I would make an array that contained the expected field names. Then user a foreach loop to iterate through the array of expected field names and check if the corresponding $_POST variable is empty or not. For any that are empty, add the name of the field to an error array. You can then test if the error array is empty (no errors) or not. Since the error array contains an element with the field name for each field that was empty, you can use that information as you desire on the form to prompt for which fields must be filled in.

Link to comment
Share on other sites

a switch would not work in this situation as the values need to be compared to a common output, you may simply try making each of those conditions if statments and then adding an error to an array for each of the problematic fields then parse that array to return the relevant information to the user.

 

p.s. beaten :P.

Link to comment
Share on other sites

Code that demonstrates the method I suggested above -

<?php
// 'required' field validation -
$fields = array(); // array of expected field names
$fields[] = 'firstName';
$fields[] = 'lastName';
$fields[] = 'day';
$fields[] = 'month';
$fields[] = 'year';
$fields[] = 'country';
$fields[] = 'emailAddress';
$fields[] = 'username';
$fields[] = 'password';

$empty_fields = array(); // array to hold list of empty fields
foreach($fields as $field){
if(empty($_POST[$field])){
	$empty_fields[] = $field;
}
}
if(empty($empty_fields)){
// all fields contained something
// process the form data here...
} else {
// at least one field was empty
// pretend this is where the form is output -
echo "The following fields were empty, please enter the requested infomration -<br />";
foreach($empty_fields as $field){
	echo "$field<br ?>";
}
}
?>

 

Once you have an array of the expected field names you can also use that information to use php to dynamically produce your form.

Link to comment
Share on other sites

Hi all,

 

I basically want to check if every single one of the forms fields is filled out. If not return an error...

The below works.. However, I cannot give out individual response ie. you still need to fill in your firstname and email address.

Is it a switch statement I'm looking for, and can I have a good example of how I can implement it into my problem?

 

<?php
    if ($emailAddressValidation>0){
        echo "Email address already taken";
    }
    else if ($usernameValidation>0){
        echo "Username already taken";
    }
    else if ($firstName = '' && $lastName = '' && $day = '' && $month = '' && $year = '' && $country = '' && $emailAddress = '' && $username = '' && $password = ''){    
        mysql_query("insert into userInformation(firstName, lastName, day, month, year, country, emailAddress, username, password) 
            values('$firstName','$lastName','$day','$month','$year','$country','$emailAddress','$username','$convertedPassword')");
    }
    else{
        echo "You have missed out one or more of the following required fields";
    }
?>

 

heya.  looks like you already got some great advice, but I'll share how I do it just for funsies.

 

I basically check one thing at a time.  If there's a field that doesn't validate, I set the session variable to a custom message for that field.  Example, if the name field is empty, I set:

$_SESSION['FormValidation'] = 'Error.  The name field is required.';

 

Then I send them back to the form page.

 

Then on the form page, I have an if statement that checks to see if that session variable is empty or not.  If it's not emtpy, I echo it out.  I also wrap my form boxes with a span or div with a couple pixels of padding, and change the class that sets the background color to be the color red if that session variable contains the message tied to that field.  That way, the field the error was in is highlighted with red so they can find it quicker.  After checking it I always unset that session variable so if they reload the page it'll clear the errors.  Also, since I save ALL my fields to session variable, when they get sent back, I autofill the information they had when they submitted so they don't have to type it all again. That's as easy as checking to see if each session variable is emtpy or not, if it's not, then you set value="' . $_SESSION['Name'] . '" etc etc

 

Lots of ways of doing it, that's just my method.  Good luck!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.