tim87 Posted February 1, 2010 Share Posted February 1, 2010 Hi, I've just made the move to OO and have a problem printing an error message from a login script. login.php contains my form and retrieves the username and password. Calls the authentication class and passes username and pass to the Login object. login.php $login = new authentication(); if(isset($_POST['login'])){ $username = $_POST[user]; $pass = $_POST[pass]; $login->Login($username, $pass); } classes.php public function LoginError($error){ echo $error; } public function Login($username, $pass){ $db = new Database; $Query = "SELECT * from user where Username='$username' AND Password='$pass'"; $db->query($Query); $db->singleRecord(); if($db->numRows() > 1){ $this->LoginError("Login error please contact Adminstraitor"); } elseif ($db->numRows() == 0){ $this->LoginError("Username or Password incorrect, please try again"); } else{ header( "Location: home.php" ); } } What I am trying to achieve is to print the LoginError in a div in login.php <div id="content"><?php $login->LoginError();?></div> But instead of appearing on the page in the div it appears at the top of the page above my header (forcing my header down). Can anyone explain/show me how to print the error message within my HTML? Thanks in advance Tim Quote Link to comment https://forums.phpfreaks.com/topic/190527-oo-php-passing-variable-from-class-to-html/ Share on other sites More sharing options...
KevinM1 Posted February 1, 2010 Share Posted February 1, 2010 I'd add a bit of value checking and clearer methods to your class, because right now you're asking LoginError to do a bit too much (both registering the error and displaying it): <?php class Authentication { private $error; private function hasError() { if (!empty($this->error)) { return true; } else { return false; } } public function displayError() { if ($this->hasError()) { echo $this->error; } } public function Login($username, $pass) { $db = new Database(); $query = "SELECT * from user where Username='$username' AND Password='$pass'"; $db->query($query); $db->singleRecord(); if ($db->numRows() > 1) { $this->error = "Login error: Please contact Adminstraitor"; } elseif ($db->numRows() == 0) { $this->error = "Username or Password incorrect, please try again"; } else { header( "Location: home.php" ); } } } ?> <div id="content"><?php $login->displayError(); ?></div> If that doesn't help, show more code. Quote Link to comment https://forums.phpfreaks.com/topic/190527-oo-php-passing-variable-from-class-to-html/#findComment-1004955 Share on other sites More sharing options...
tim87 Posted February 1, 2010 Author Share Posted February 1, 2010 Thanks Nightslyr, I can now see where I was going wrong. thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/190527-oo-php-passing-variable-from-class-to-html/#findComment-1004974 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.