Kimomaru Posted June 25, 2012 Share Posted June 25, 2012 Hello, I have a simple form for user registration that, right now, requires that all fields be filled out. I would like to change the php code so that two of the fields will not throw an error if they're not filled out (date of birth and middle name), but the rest should be mandatory. The code looks like this; $FirstName=$_POST['FirstName']; $MiddleName=$_POST['MiddleName']; $LastName=$_POST['LastName']; $DateOfBirth=$_POST['DateOfBirth']; $Email=$_POST['Email']; $UserName=$_POST['UserName']; $Password=$_POST['Password']; $Password2=$_POST['Password2']; session_start(); try { if (!filled_out($_POST)) { throw new Exception('You have not filled the form out correctly - please go back and try again.'); } _______________________________ function filled_out($form_vars) { foreach ($form_vars as $key => $value) { if ((!isset($key)) || ($value == '')) { return false; } } return true; } My guess is that the code that needs to be changed is the part that's in bold, but I'm not sure how I can change it to skip the two values that I would like to make optional. Can someone help? Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 25, 2012 Share Posted June 25, 2012 You'd want to add a list of the fields which are allowed to be blank, in an array, and then pass it to the function. Then when you check each form variable, also check that array to see if it's in there. Quote Link to comment Share on other sites More sharing options...
Kimomaru Posted June 25, 2012 Author Share Posted June 25, 2012 Cool, thanks Jesirose. 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.