Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Feb 2011
    Posts
    114
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Help me on oop code here

    Hi ,

    I have this code ..and when a form is field correctly and submit, still the 'Please enter all fields are required' error message display.

    How do i make it not display when the form is field correctly.
    Here is the code

    PHP Code:
      //learning of oop
     
    class Users{

        public 
    $name;
        public 
    $email;
        public 
    $password;
        public 
    $confirm_pass;

        
    //checking for name,email,password
       
    public function CheckUserInput($name,$email,$password){

         if(isset(
    $_POST['name']) && isset($_POST['email']) && isset($_POST['password'])
          && isset(
    $_POST['confirm_pass'])){
         
    $this->name=$_POST['name'];
         
    $this->email=$_POST['email'];
         
    $this->password=$_POST['password'];
         
    $this->confirm_pass=$_POST['confirm_pass'];

          if(!empty(
    $this->name) && !empty($this->email) && !empty($this->password)
           &&!empty(
    $confirm_pass)){

            echo 
    'OK';
          }else{
          echo 
    'Please enter all fields are required'.'<br>';
          }

         }
      }


        
    //checking for confirm password
      
    public function ConfirmPass($confirm_pass){
        if(!(
    $this->password==$this->confirm_pass)){
         echo 
    'Password do not much';
        }

      }


     }

     
    $UserInput= new Users;


     
    $UserInput->CheckUserInput('name','email','password');
      
    $UserInput->ConfirmPass('confirm_pass'); 

    <form action="index.php" method="POST">
    Your Name:<br> <input type="text" name="name"> <br>
    Your Email:<br> <input type="email" name="email"><br>
    Your Passwword:<br> <input type="password" name="password"><br>
    confirm Password:<br> <input type="password" name="confirm_pass"><br>

    <input type="submit" value="Submit">
    </form>

    Thanks

  • #2
    New to the CF scene
    Join Date
    Jan 2012
    Location
    PL
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi.

    Change var $confirm_pass to $this->confirm_pass in empty() function. It should help.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Problem is you are mixing around the member properties, arguments and superglobals. $confirm_pass doesn't exist within the scope of this method, so empty is always true.

    This signature is bizarre too. You accept three required parameters but you don't do anything with them. I'd also recommend avoiding superglobals within object methods when you can to maximize the reuse. Exceptions may also be used to force terminate the method handlings.
    PHP Code:
    public function CheckUserInput($name$email$password$confirm_pass)
    {
        if (empty(
    $name) || empty($email) || empty($password) || empty($confirm_pass))
        {
            throw new 
    Exception('All fields are required.'); // Or handle via booleans instead
        
    }
        
    $this->name $name;
        
    $this->email $email;
        
    $this->password $password;
        
    $this->confirm_pass $confirm_pass;
    }

    public function 
    ConfirmPass()
    {
        return !
    strcmp($this->password$this->confirm_pass);
    }

    try
    {
        
    $ui = new User();
        
    $ui->checkUserInput($_POST['name'], $_POST['email'], $_POST['password'], $_POST['confirm_pass']);
        if (
    $ui->confirmPass())
        {
            print 
    'Verfied';
        }
        else
        {
            print 
    'Passwords do not match';
        }
    }
    catch (
    Exception $ex)
    {
        print 
    $ex->getMessage();


  • #4
    Regular Coder
    Join Date
    Feb 2011
    Posts
    114
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi Sir;

    Thanks much for your reply.You are really a GURU.
    I am confused with your code but work perfectly.

    Thanks much
    Clement Osei


  •  

    Posting Permissions

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