www.webdeveloper.com
Results 1 to 3 of 3

Thread: help with if statement registration

  1. #1
    Join Date
    Jun 2014
    Posts
    16

    help with if statement registration

    I am trying to create a script to register a user. I first check to make sure the input is not empty and the input is good I can open a connection and begin the registering.
    I have this code here but I can't seem to figure out how I can check if there are no errors.

    PHP Code:
    public function registerUser() {
    if (empty(
    $_POST['reg_username'])) {
    $this->errors[] = "Please choose a username";
    }
    if (empty(
    $_POST['reg_password'])) {
    $this->errors[] = "Please choose a password";
    }
    if (empty(
    $_POST['reg_password_repeat'])) {
    $this->errors[] = "Make sure you enter your password in both fields";

    I want it work so that if any of the fields are empty the function will return the errors and stop executing the rest of the function.

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    817
    So how are you calling this function/object method? Show us that code. Since you have an array to hold your error messages, when you return to the caller, you can simply check if that array is not empty and then send back your login page with the messages embedded in it.

  3. #3
    Join Date
    May 2014
    Posts
    1,085
    ginerjm has it right in that we really need to see more to say for certain -- BUT:

    why not just:

    if (count($this->errors) > 0)

    That would be your error state.

    Oh, one thing I like to do is when there's an error to be added to an array of errors, use the field that had the error as the index.

    Code:
    if (empty($_POST['reg_username'])) { 
      $this->errors['reg_username'] = "Please choose a username"; 
    }
    That way when you re-send the populated form on error (you are doing that, right?) you can put the error next to the field that has the error. Likewise you can use "isset($this->errors['reg_username'])" to add a 'error' class to the input to do things like color/highlight it.
    Java is to JavaScript as Ham is to Hamburger.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles