Jump to content

Modifying a function


Kimomaru

Recommended Posts

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?

Link to comment
Share on other sites

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.

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.