Click here to Skip to main content
15,911,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
i am using exceptions in my user class .when i catch them the message is automatically displayed and form goes to my header if i leave fields empty . what should i do ???
here is my class code :
<?php
class usernew1
{

    private $firstName;
    private $middleName;
    public function __construct()
    {
    }
    public function  __set($name,$value)
    {
        $method = "set_".$value;
        if(!method_exists($this, $method))
        {
            throw new Exception("property does not exist ...");
        }
        $this->$method($value);
    }
    public function __get($name)
    {
        $method = "get_".$name;
        if(!method_exists($this,$method))
        {
            throw new Exception("property does not exist...");
            }
            return $this->$method();
    }
    private function  set_firstName($firstName)
    {
        $reg = "/^[a-z]+$/i";
        if(!preg_match($reg, $firstName))
        {
            throw new Exception("missing /invalid first name ....");
        }
        $this->firstName =$firstName;
    }
    private function get_firstName()
    {
        return $this->firstName;
    }
}
?>
next is my process page to catch exceptions
process:
<?php
require_once("usernew1.php");
session_start();
$objuser = new usernew1();
$errors = array();
try
{
    $objuser->__set("firstName",$_POST['firstName']);
}
catch(Exception $ex)
{
    $errors['firstName'] = $ex->getMessage();
}
?>
and in the end  form is here .....

<?php
//require_once("webinterface.php");
require_once("usernew1.php");
session_start();
if(isset($_SESSION['errors']))
{
        $errors = $_SESSION['errors'];
}
else if(isset($_SESSION['objuser']))
{
        $objuser = unserialize($_SESSION['objuser']);
}
else
{
    $objuser = new usernew1();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html xmlns="http://www.w3.org/1999/xhtml">
&lt;head>
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
&lt;title>User Registration&lt;/title>
<link href="styles/newUserStyles.css" type="text/css" media="all" rel="stylesheet" />
&lt;/head>

&lt;body>
<div id="formContainer">

<div id="headingRow">New User Registration</div>

&lt;form action="process.php" method="post" enctype="multipart/form-data" id="userForm">

<div class="row">
    <div class="cell cellLeft">First Name</div>
    <div class="cell cellRight">
    &lt;input type="text" id="firstName" name="firstName" value=""/><span id="firstNameError">
<?php
if(isset($errors['firstName']))
{
    echo($errors['firstName']);
}
?></span></div><div class="clearBox"></div>
<div class="clearBox"></div>
<div class="row c2" style="text-align:center;">
&lt;input type="submit" value="Register" /></div>

&lt;/form>
</div>
&lt;/body>
&lt;/html>

kindly help me and poin out my mistakes here . i am new in php.
thanks in advance
Posted
Comments
Killzone DeathMan 28-Jun-13 9:24am    
Maybe you need to get more sleep and smoking tobacco instead of marijuana :P
I hope I've helped!
Regards,
KZ

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900