View Full Version : Help me on oop code here

01-19-2012, 06:01 PM
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

//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'])){

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

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


//checking for confirm password
public function ConfirmPass($confirm_pass){
echo 'Password do not much';



$UserInput= new Users;


<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">


01-19-2012, 06:11 PM

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

01-19-2012, 06:13 PM
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.

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);

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

01-19-2012, 06:54 PM
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