Page 1 of 1

echo errors in wrong Place [SOLVED]

Posted: Mon May 07, 2012 10:57 am
by D_zone
So my problem is this, I got a registration form BUT every time I got an error it display it above the <h1> Registration </h1> title I have in my form. My goal is to place the errors Below the Registration title. I have a class called register and the function that display errors in it. This is the code;

Code: Select all

if(isset($_POST['submit']))
{
  include_once('register.php');

  $register = new Register();
  if($register->process())
    header("Location:http://www.google.com");
  else
   $error_mess = $register->display_errors();
}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Registration</title>
<link rel="stylesheet" type="text/css" href="../style.css" /> 

<body>
<h1> Registration </h1>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> 

 </div>
 <?php 
 
if ($error_mess) { echo   $error_mess ;} 
?>

...The rest of the form HTML code...
function display_errors

Code: Select all

 
public function display_errors()
  {
    echo "<h3>Errors</h3>";

    foreach($this->errors as $key=>$value)
      echo $value."<br>";
  }
Thank you in advance for future replies

Re: echo errors in wrong Place

Posted: Mon May 07, 2012 11:12 am
by Celauran
display_errors is broken. It echoes errors instead of returning them.

Re: echo errors in wrong Place [SOLVED]

Posted: Mon May 07, 2012 4:00 pm
by D_zone
Hi Celauran, thank you for replying, I fixed the broken function by adding a return variable.

Code: Select all

public function display_errors()
  {
    $errorx =  "<h3>Errors</h3>";

    foreach($this->errors as $key=>$value)
      $errorx .= $value."<br>";
    	return $errorx ;
  }

Re: echo errors in wrong Place [SOLVED]

Posted: Tue May 08, 2012 7:05 am
by tom12j
This is not a good programming practice. Why you are not using exception handling?