jimmyt1988 Posted December 8, 2009 Share Posted December 8, 2009 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"; } ?> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 8, 2009 Share Posted December 8, 2009 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. Quote Link to comment Share on other sites More sharing options...
FaT3oYCG Posted December 8, 2009 Share Posted December 8, 2009 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 . Quote Link to comment Share on other sites More sharing options...
jimmyt1988 Posted December 8, 2009 Author Share Posted December 8, 2009 Thanks for the suggestions.. I'll have a play with those techniques! Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 8, 2009 Share Posted December 8, 2009 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. Quote Link to comment Share on other sites More sharing options...
jimmyt1988 Posted December 8, 2009 Author Share Posted December 8, 2009 Wow, thanks for the example. I was so far off with my attempt. Quote Link to comment Share on other sites More sharing options...
5kyy8lu3 Posted December 8, 2009 Share Posted December 8, 2009 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! Quote Link to comment Share on other sites More sharing options...
jimmyt1988 Posted December 8, 2009 Author Share Posted December 8, 2009 Thanks for all the help. I got it all working with it even putting star next to one that needs editing. Looks brilliant. Thanks for all the help. Great community here. Quote Link to comment 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.