Click here to Skip to main content
15,921,279 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
<?php
$fname = $lname = $email = $phone = $gender = $username = $password = '';
$fnameErr = $lnameErr = $emailErr = $phoneErr = $genderErr = $usernameErr = $passwordErr = '';
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $fname = test_input($_POST['fname']);
    $lname = test_input($_POST['lname']);
    $email = test_input($_POST['email']);
    $gender = test_input($_POST['gender']);
    $phone = test_input($_POST['phone']);
    $username = test_input($_POST['username']);
    $password = test_input($_POST['password']);
}
    function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    if(!$_POST['fname']) {
    $fnameErr = 'Firstname is required. (Only letters and whitespace)';
    } else {
        $fname = test_input($_POST['fname']);
if(!preg_match("/^[a-zA-Z]*$/", $fname))
        {
        echo $fnameErr;
    }
}

    if(empty($_POST['lname'])) {
        $lnameErr = 'Last Name is required';
        } else {
            $lname = test_input($_POST['lname']);

            if(!preg_match("/^[a-zA-Z]*$/", $lname))
            {
                echo $lnameErr;
            }
        }

        if(empty($_POST['email'])) {
            $emailErr = 'You must provide an email';
        } else {
            $email = test_input($_POST['email']);

            if(!filter_var($email, FILTER_VALIDATE_EMAL))
            {
                echo 'Invalid email format';
            }
        }

        if(empty($_POST['gender'])) {
            $genderErr = 'You must select your gender';
        } else {
            $gender = test_input($_POST['gender']);
        }

        if(empty($_POST['phone'])) {
            $phoneErr = 'You must provide an phone number';
        } else {
            $phone = test_input($_POST['phone']);
        }

        if(empty($_POST['username'])) {
            $usernameErr = 'You must provide a username';
        } else {
            $username = test_input($_POST['username']);
        }

        if(empty($_POST['password'])) {
            $passwordErr = 'You must provide a password';
        } else {
            $password = test_input($_POST['password']);
        }
    }
?>

<!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" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252;">
<title>Register | </title>
<meta name="title" content="Registration Form w"/>
<meta name="description" content=""/>
<meta name="keywords" content="Register with us"/>

<link href="css/style_demo.css" rel="stylesheet" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>


</head>
<body>
<div id="bodyfull">
<div id="bodyfull2">
    <div id="center">

        <div class="inner_right_demo">
<form name="register" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" id="register" />
            <div class="form_box">
                <div>
                    <label>First Name</label>
                    <input type="text" placeholder="Enter Your First Name" id="fname" name="fname" value="<?php echo $fname; ?>" required="required" /><br />
                    <span class="error"><?php echo $fnameErr; ?></span>
                </div>

                <div>
                    <label>Last Name</label>
                    <input type="text" placeholder="Enter Your Last Name" id="lname" name="lname" required="required" /><br />
                    <span> <?php echo $lnameErr;  ?> </span>
                </div>

                <div>
                    <label>Email</label>
                    <input type="email" placeholder="Enter Your Email Address" id="email" name="email" required="required" /><br />
                    <span> <?php echo $emailErr;  ?> </span>
                </div>

                <div>
                    <label>Phone</label>
                    <input type="text" placeholder="Enter Your Phone Number" id="phone" name="phone" /><br />
                    <span> <?php echo $phoneErr;  ?> </span>
                </div>

                <div>
                    <label>Gender</label>
                    <div class="otherinputs"><input type="radio" value="Male" checked name="gender"> <span>Male</span> <input type="radio" value="Female" name="gender" /> <span>Female</span> <br />
                    <span> <?php echo $genderErr;  ?> </span> </div>
                </div>

                <div>
                    <label>User Name</label>
                    <input type="text"  placeholder="Enter Your User Name" id="username" name="username" required="required" /><br />
                    <span> <?php echo $usernameErr;  ?> </span>
                </div>

                <div>
                    <label>Password</label>
                    <input type="password"  placeholder="Enter Your Password" id="password" name="password" required="required" /><br />
                    <span> <?php echo $passwordErr;  ?> </span>
                </div>

                <div>
                    <label>Confirm Password</label>
                    <input type="password"  placeholder="Enter Your Password Again" id="cpassword" name="cpassword" required="required" />
                </div>

                <div>
                    <label>Captcha</label>
                    <input type="text" placeholder="Enter Code" id="captcha" name="captcha" class="inputcaptcha" required="required" />
                    <img src="demo_captcha.php" class="imgcaptcha" alt="captcha"  />
                    <img src="images/refresh.png" alt="reload" class="refresh" />
                </div>

                <div>
                    <label> </label>
                    <div class="otherinputs" ><input type="submit" value="Submit" name="B1" class="submit">    <input type="reset" value="Reset" name="B2" class="submit" /></div>
                </div>





            </div>
            </form>
        </div>

</div>
</div>
</div>
</body>

</html>
Posted
Comments
OriginalGriff 2-Dec-15 3:31am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
So tell us what it's trying to validate, what you entered, what results you got.
Use the "Improve question" widget to edit your question and provide better information.
blancos_khim 2-Dec-15 3:36am    
ok! what i'm trying to do is validate the user's data from the form so that i can store it into a database. The problem is that if the inputs are empty, the form won't throw the error messages. My question is what is not coded correctly.
I hope that is sufficient now.

1 solution

You need to really validate your data.

For example, to see if the data was sent back at all, use isset($_POST['whatever']).
However, you may also wish to reject empty items, and there are other things for witch you wish to validate.

Check Here[^] and the links contained therein.

Ideally, you'd want to validate before the trip to the server - I normally do - and for this you may wish to consider javaScript: http://www.w3schools.com/jsref/event_onsubmit.asp[^] and pre-handle the contents of your <form> (if your data's coming from a <form>!)



 
Share this answer
 
v2

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