My Validation Doesnt Work! :-(

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adambob
Forum Newbie
Posts: 2
Joined: Sun May 11, 2008 5:33 am

My Validation Doesnt Work! :-(

Post by adambob »

Hello peeps,

I have come to a stumbling block on my validation for a registration form, and wondered if you could kindly point me in the direction of the solution. Firstly here is my code;

<?php

$fname_x = $sname_x = $email_x = $password_x = $password_c = $capch_x = "txta";
if ($_POST)
{
$error = "";
if(empty($FNAME))
{
$error = "<b>Please enter a first name.</b><br/>";
echo"$error";
$fname_x ="error";
}

if(empty($SNAME))
{
$error = "<b>Please enter a surname.</b><br/>";
echo"$error";
$sname_x ="error";
}

if(empty($EMAIL))
{
$error = "<b>Please enter a email address.</b><br/>";
echo"$error";
$email_x ="error";
}

elseif(!eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$',$EMAIL))
{
$error = "<b>Please enter a valid email address</b><br/>";
echo"$error";
$email_x ="error";
}

if(empty($PASSWORD))
{
$error = "<b>Please supply a password.</b><br/>";
echo"$error";
$password_x ="error";
}

elseif($PASSWORDC != $PASSWORD)
{
$error = "<b>Please ensure both passwords match.</b><br/>";
echo"$error";
$password_c ="error";
}
if(md5($_POST['security']) != $_SESSION['key'])
{
$error = "<b>Please enter the security code correctly</b>";
echo"$error";
$capch_x ="error";
}
}
?>

The Problem;

Everything seems to work fine up until the point all my corrections have been rectified, i.e. text in required fields, passwords match, but it skips the email validation. So for example if i miss a required field like name, but i have an incorrect email in the email field it will pick up email as invalid, but than when i input a name for example, it completly skips the email validation. This occurs on all fields where its just text, for example, forename, surname, password, if any of these fields are blank and email is formulated incorrectly it will say Hey, please enter text in required fields, and enter a correct email address, put once i input text in required fields, it submits the form completly skipping the email validation :-(.

Can someone please assist me of this matter!

Many thanks for help in advance!

Adambob
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: My Validation Doesnt Work! :-(

Post by califdon »

If you would please use the Code tags (click on the Code button in the Post A Reply page) to enclose your code, it would make it a great deal easier for us to read your code. Assuming that you have used conventional indenting practices, it should be fairly easy to see where your conditional logic is breaking down. If you are NOT using indentation (which is how it appears when you post as you did), shame on you--start doing it! This is exactly why you should. You also need to show us the code that is returning your user to the script that displays the form, if there is a validation problem.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: My Validation Doesnt Work! :-(

Post by Jade »

Have you tried checking the email before the if statements? It sounds like something about having that function there is messing with the rest of the if-statements.
adambob
Forum Newbie
Posts: 2
Joined: Sun May 11, 2008 5:33 am

Re: My Validation Doesnt Work! :-(

Post by adambob »

Please accept my apology, I do confirm to coding standards, but I forgot to press the code button due to my inexperience of these message boards. But I do enclose my code, in the hope you now may be able to identify my logic problem.

I have tried adding the email validation script as a separate entity, out of the other if statements scope, but I still receive the same problem, it processing registration without validating the email when required fields are entered. :-(

Please advise many thanks for your help.

Adam

Code: Select all

 
<?php
require "../connect.php";
 
    include("../input_cl.php");
    $FNAME = $_POST['fname'];
    $SNAME = $_POST['sname'];
    $EMAIL = $_POST['email'];
    $PASSWORD = $_POST['password'];
    $PASSWORDC = $_POST['password_c'];
        
    
    if((!$FNAME) || (!$SNAME) ||(!$EMAIL) ||(!$PASSWORD) ||(!$PASSWORDC))
    {   
        ?>
        <!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=iso-8859-1" />
        <title>
        Untitled Document
        </title>
 
        <link rel="stylesheet" href="../style.css" type="text/css"/>
        <!--[if lte IE 6]><style type="text/css" media="screen">@import "../style2.css";</style><![endif]-->
        <!--[if gte IE 7]><style type="text/css" media="screen">@import "../style.css";</style><![endif]-->
        </head>
        <body>
        <div id="main">
        <?php include ("../header.html") ?>
        <div class="menu">
        <?php include ("../menu.php") ?>
        </div>
        <div class="content">
        <h2>Register</h2>
        <fieldset class="picturewrap">
        <legend><b>Registration Form</b></legend>
        <?php
        $fname_x = $sname_x = $email_x = $password_x = $password_c = $capch_x = "txta";
        if ($_POST)
        {
            $error = "";
            if(empty($FNAME))
            {
                $error = "<b>Please enter a first name.</b><br/>";
                echo"$error";
                $fname_x ="error";
            }
    
            if(empty($SNAME))
            {
                $error = "<b>Please enter a surname.</b><br/>";
                echo"$error";
                $sname_x ="error";
            }
    
            if(empty($EMAIL))
            {
                $error = "<b>Please enter a email address.</b><br/>";
                echo"$error";
                $email_x ="error";
            }
    
            elseif(!eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$',$EMAIL))
            {
                $error = "<b>Please enter a valid email address</b><br/>";
                echo"$error";
                $email_x ="error";
            }
                        
            if(empty($PASSWORD))
            {
                $error = "<b>Please supply a password.</b><br/>";
                echo"$error";
                $password_x ="error";
            }
                        
            elseif($PASSWORDC != $PASSWORD)
            {
                $error = "<b>Please ensure both passwords match.</b><br/>";
                echo"$error";
                $password_c ="error";
            }
            
            if(md5($_POST['security']) != $_SESSION['key'])
            {
                $error = "<b>Please enter the security code correctly</b>";
                echo"$error";
                $capch_x ="error";
            }
        }
        ?>
 
    
                    <form id="register" name="register" method="post" action="<?php echo$_SERVER['../register']?>">
                    <fieldset class="boboo">
                    <legend><b>Personal Details</b></legend>
                    <div>
                    <label for="email">First Name:</label>
                    <input type="text" name="fname" id="fname" class="<?php echo $fname_x?>" value="<?php echo $FNAME?>"  /><b>?</b>
                    </div>
                    <div>
                    <label for="email">Surname:</label>
                    <input type="text" name="sname" id="sname" class="<?php echo $sname_x?>" value="<?php echo $SNAME?>"  /><b>?</b>
                    </div>
                    <div>
                    <label for="city">City:</label>
                    <select name="select" class="slta">
                    <option>Please Select..</option>
                    <option value='Aberdeen' >Aberdeen</option>
                    <option value='Aberystwyth' >Aberystwyth</option>
                    <option value='Andover' >Andover</option>
                    </select><b>?</b>
                    </div>
                    </fieldset>
                    <fieldset class="boboo">
                    <legend><b>Login Details</b></legend>
                    <div>
                    <label for="email">Email Address:</label>
                    <input type="text" name="email" id="email" class="<?php echo $email_x?>" value="<?php echo $EMAIL?>"  /><b>?</b>
                    </div>
                    <div>
                    <label for="password">Password:</label>
                    <input type="password" name="password" id="password" class="<?php echo $password_x?>" value="<?php echo $PASSWORD?>" /><b>?</b>
                    </div>
                    <div>
                    <label for="password">Comfirm Password:</label>
                    <input type="password" name="password_c" id="password_c" class="<?php echo $password_c?>" value="<?php echo $PASSWORDC?>" />
                    </div>
                    </fieldset>
 
 
                    <fieldset class="boboo">
                    <legend><b>Security Check</b></legend>
                    <div>
                    <label for="password">Security Image:</label>
                    <img src="adam.php" border="0">
                    </div>
                    <div>
                    <label for="password">Image Characters:</label>
                    <input type="text" name="security" id="security" class="<?php echo $capch_x?>" /><b>?</b>
                    </div>
                    </fieldset>
 
                    <fieldset class="boboo">
                    <legend><b>Please Confirm Registration</b></legend>
                    <div>
                    <label for="terms">Please accept our terms of use:</label>
                    <input type="checkbox" name="checkbox" id="terms" />
                    </div>
                    <div>
                    <label for="confirm" class="clearfix">-</label>
                    <input type="submit" name="register" id="register" value="Register" class="btna" />
                    </div>
                    </fieldset>
                    </fieldset>
                    </form>
 
                    <br />
              </div>
              <div class="footer">
                        &copy;Copyright ACD Group 2008 - All rights reserved
                        <?php include ("../footer.html") ?>
              </div>
 
            </div>
 
        </body>
</html>
 
<?php
}
else
{
 
    $query = ("INSERT INTO customer (first_name, surname, email_address, password) VALUES ('$FNAME', '$SNAME', '$EMAIL', '$PASSWORD')");
    $result = @mysql_query($query, $connection)
    or die ("Unable to perform query <br> $query");
 
    mysql_close($con);
    header("Location:../login?target=new");
    exit();
    
}
?>
 
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: My Validation Doesnt Work! :-(

Post by califdon »

I extracted the logic from your script to come up with this for simpler analysis:

Code: Select all

    if((!$FNAME) || (!$SNAME) ||(!$EMAIL) ||(!$PASSWORD) ||(!$PASSWORDC))
     { 
    //display header and menu
         if ($_POST)
         {
             if(empty($FNAME))
             {
        //create error string
             }
    
             if(empty($SNAME))
             {
        //create error string
             }
    
             if(empty($EMAIL))
             {
        //create error string
             }            
                elseif (!eregi('^[_a-z0-9-]+  ...  [a-z]{2,3})$',$EMAIL))
             {
                 //create error string
             }
                        
             if(empty($PASSWORD))
             {
                //create error string
             }                        
                elseif($PASSWORDC != $PASSWORD)
             {
               //create error string
             }
            
             if(md5($_POST['security']) != $_SESSION['key'])
             {
        //create error string
             }
         }
    //display registration form
     }
     else
     {
        //insert into table
     }
I'm not familiar with the syntax you used in the first IF statement, (!$FNAME) and so on, although it may be correct; I would think that it perhaps should be: !empty($FNAME). Likewise, I'm not familiar with the syntax, if ($_POST), although it, too, could be correct.

Then, it's not clear to me how you expect this logic to work. You are creating these error variables, like $fname_x, but I don't see that you ever do anything with them. Did you intend to check them at some point and take action based on whether they contain the string "error"?
Post Reply