Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 41
  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post

    PHP Registration form validation

    I have a registration page that i am using to enable users to register to a website that i am developing. I have basic code the adds to the database but and has basic validation, the error messages appear on another page and i would would like them appearing below the form once the submit button is clicked, can someone please help.

    my code is:

    <?php
    session_start();
    include("database.php");

    function usernameTaken($username){
    global $conn;
    if(!get_magic_quotes_gpc()){
    $username = addslashes($username);
    }
    $q = "select username from users where username = '$username'";
    $result = mysql_query($q,$conn);
    return (mysql_numrows($result) > 0);
    }

    function emailTaken($email){
    global $conn;
    if(!get_magic_quotes_gpc()){
    $email = addslashes($email);
    }
    $q = "select username from users where email = '$email'";
    $result = mysql_query($q,$conn);
    return (mysql_numrows($result) > 0);
    }


    function addNewUser($username, $password, $forename, $surname, $email){
    global $conn;
    $q = "INSERT INTO users VALUES ('$username', '$password', '$forename', '$surname', '$email', '$mobile', '$dob', '$location')";
    return mysql_query($q,$conn);
    }

    function displayStatus(){
    $uname = $_SESSION['reguname'];

    if($_SESSION['regresult']){
    ?>

    <h1>Registered!</h1>
    <p>Thank you <b><?php echo $uname;?></b>, your information has been added to the
    database, you may now <a href="index.php" title="Login">log in</a>.</p>

    <?php
    }
    else{
    ?>

    <h1>Registration Failed</h1>
    <p>We&#39;re sorry, but an error has occurred and your registration for the username <b><? echo $uname; ?></b>,
    could not be completed.<br>
    Please try again at a later time.</p>

    <?php
    }
    unset($_SESSION['reguname']);
    unset($_SESSION['regforename']);
    unset($_SESSION['regsurname']);
    unset($_SESSION['regemail']);
    unset($_SESSION['regmobile']);
    unset($_SESSION['regdob']);
    unset($_SESSION['reglocation']);
    unset($_SESSION['registered']);
    unset($_SESSION['regresult']);
    }

    if(isset($_SESSION['registered'])){
    ?>

    <html>
    <title>Registration Page</title>
    <body>

    <?php displayStatus(); ?>

    </body>
    </html>

    <?php
    return;
    }

    if(isset($_POST['subjoin'])){
    /* Make sure all fields were entered */
    if(!$_POST['user'] || !$_POST['pass'] || !$_POST['forename'] || !$_POST['surname'] || !$_POST['email'] || !$_POST['mobile'] || !$_POST['dob'] || !$_POST['location'] ){
    die('You didn\'t fill in a required field.');
    }

    /* Spruce up username, check length */
    $_POST['user'] = trim($_POST['user']);
    if(strlen($_POST['user']) > 30){
    die("Sorry, the username is longer than 30 characters, please shorten it.");
    }

    /* Check if username is already in use */
    if(usernameTaken($_POST['user'])){
    $use = $_POST['user'];
    die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one.");
    }

    /* Check if email is already in use */
    if(emailTaken($_POST['email'])){
    $use = $_POST['email'];
    die("Sorry, the email: <strong>$use</strong> is already taken, please pick another one.");
    }



    /* Add the new account to the database */
    $md5pass = md5($_POST['pass']);
    $_SESSION['reguname'] = $_POST['user'];
    $_SESSION['regforname'] = $_POST['forename'];
    $_SESSION['regsurname'] = $_POST['surname'];
    $_SESSION['regemail'] = $_POST['email'];
    $_SESSION['regemail'] = $_POST['mobile'];
    $_SESSION['regemail'] = $_POST['dob'];
    $_SESSION['regemail'] = $_POST['location'];
    $_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass, $_POST['forename'], $_POST['surname'], $_POST['email'], $_POST['mobile'], $_POST['dob'], $_POST['location']);
    $_SESSION['registered'] = true;
    echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
    return;
    }
    else{
    ?>
    <?php
    include "registerform.php";
    ?>
    <?php
    }
    ?>



    And the form is:


    <form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
    <table align="left" border="0" cellspacing="0" cellpadding="3" width="568" id="table15">
    <tr><td width="216"><span class="navyboldtxt">Username:</span></td>
    <td align="left" width="340"><input type="text" name="user" maxlength="30" size="35"></td></tr>

    <tr><td width="216"><span class="navyboldtxt">Password:</span></td><td align="left" width="340">
    <input type="password" name="pass" maxlength="30" size="36"></td></tr>

    <tr><td width="216"><span class="navyboldtxt">Forename:</span></td><td align="left" width="340">
    <input type="text" name="forename" maxlength="30" size="35"></td></tr>

    <tr><td width="216"><span class="navyboldtxt">Surname:</span></td><td align="left" width="340">
    <input type="text" name="surname" maxlength="30" size="35"></td></tr>

    <tr><td width="216"><span class="navyboldtxt">Email</span></td><td align="left" width="340">
    <input type="text" name="email" maxlength="30" size="35"></td></tr>

    <tr><td width="216"><span class="navyboldtxt">Mobile</span></td><td align="left" width="340">
    <input type="text" name="mobile" maxlength="30" size="35"></td></tr>

    <tr><td width="216"><span class="navyboldtxt">DOB</span></td><td align="left" width="340">
    <input type="text" name="dob" maxlength="30" size="35"></td></tr>

    <tr><td width="216"><span class="navyboldtxt">Location</span></td><td align="left" width="340"><select name="location">
    <option VALUE="1">Antrim
    <option VALUE="2">Armagh
    <option VALUE="3">Belfast
    <option VALUE="4">Banbridge
    </select></td></tr>
    <tr><td colspan="2" align="right">


    <input type="submit" name="subjoin" value="Join!"></td></tr>
    </table>
    </form>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</div>
    </div>



    Philip

  • #2
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    Whenever an error message needs to be displayed after the form, just put the error into a variable and echo the variable wherever you need it. Here's how I do it, personally:

    PHP Code:
    <?php
    //Top of the page, I initially set the message variable to blank
    $message '';
    $error_stat 0;


    //then process the form
    if (isset($_POST['submit'])) {
       
    //do all the code stuff

       //if there are any errors, I set $error_stat to 1 and put the errors in the $message:
       
    if (empty($_POST['name'])) {
          
    $error_stat 1;
          
    $message .= 'Please enter your name.';
       }

       
    //then, only put the info in the db if $error_stat is still 0
       
    if ($error_stat == 0) {
          
    //insert into db
       
    }
    }
    ?>

    Then put your form here

    <form method="post">
    <input type="text" name="name" />
    </form>


    <!-- then echo the $message variable from here like this -->
    <?=$message?>
    If this doesn't help enough or you need more assistance, just let us know.
    Last edited by JohnDubya; 01-31-2008 at 02:20 PM.

  • #3
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    I changed the script i was using, decided to use this one but i need some validation for the form, not sure where to put it. I want the an error message to appear beside the text field eg "please enter a username" before the form is submitted to the database and also would like to check the username does not already exist in the database.

    my script is

    <?php
    if (!isset($_POST['submit'])) {
    ?>

    <form method="post" class="regform" action="">
    <fieldset>
    <label for="username">Username:</label>
    <input name="username" type="text" id="username" onkeyup="checkUsernameForLength(this);" />
    </fieldset>


    <fieldset>
    <label for="password">Password:</label>
    <input name="password" type="password" id="password1" onkeyup="checkUsernameForLength(this);" />
    </fieldset>

    <fieldset>
    <label for="forename">Forename:</label>
    <input name="forename" type="text" id="forename" onkeyup="checkUsernameForLength(this);" />
    </fieldset>

    <fieldset>
    <label for="surname">Surname:</label>
    <input name="surname" type="text" id="surname" onkeyup="checkUsernameForLength(this);" />
    </fieldset>

    <fieldset>
    <label for="email">Email:</label>
    <input name="email" type="text" id="email" onkeyup="checkUsernameForLength(this);" />
    </fieldset>

    <fieldset>
    <label for="mobile">Mobile:</label>
    <input name="mobile" type="text" id="mobile" onkeyup="checkUsernameForLength(this);" />
    </fieldset>

    <fieldset>
    <label for="dob">DOB:</label>
    <input name="dob" id="dob" onkeyup="checkUsernameForLength(this);" />
    </fieldset>

    <fieldset>
    <label for="location">Location:</label>
    <input name="location" type="text" id="location" onkeyup="checkUsernameForLength(this);" />
    </fieldset>
    <p></p>

    <p style="text-align: center">

    <input type="submit" class="signupbtn" name="submit" value="" />

    </p>

    </form>

    <!--Register Form (Adding information) -->
    <?php
    } else {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $md5password = md5($_POST['password']);
    $forename = $_POST['forename'];
    $surname = $_POST['surname'];
    $email = $_POST['email'];
    $mobile = $_POST['mobile'];
    $dob = $_POST['dob'];
    $location = $_POST['location'];
    $ip = $_SERVER['REMOTE_ADDR'];

    mysql_query("INSERT INTO users (username, password, forename, surname, email, mobile, dob, location, ipaddress) VALUES ('$username', '$md5password', '$forename', '$surname', '$email', '$mobile', '$dob', '$location', '$ip')");
    echo "<h2>Registration Successful!</h2>";
    echo "<p>Congratulations <b>$username</b>, your registration was successful. Your account has now been created with the following details:</p>";

    echo "<p><b>Username:</b> $username</p>";
    echo "<p><b>Password:</b> $password</p>";
    echo "<p><b>Forename:</b> $forename</p>";
    echo "<p><b>Surname:</b> $surname</p>";
    echo "<p><b>Email:</b> $email</p>";
    echo "<p><b>Mobile:</b> $mobile</p>";
    echo "<p><b>DOB:</b> $dob</p>";
    echo "<p><b>Location:</b> $location</p>";

    echo "<p>You may now login.</p>";
    }
    ?>
    Last edited by PRodgers4284; 01-31-2008 at 05:59 PM.

  • #4
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    Normally, you would put your processing code at the top of the page. For instance, use the if (isset($_POST['submit'])) code at the top of the page and do the query and all that from there, and have the form beneath all this code. This allows you to create an error message if anything goes wrong or the user made an error in what they entered. So then, if the user doesn't enter a username, you can create a variable called $error_username or whatever, and you can put "Please enter your username" in that variable and echo it next to the username text field. Make sense?

  • #5
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Quote Originally Posted by JohnDubya View Post
    Normally, you would put your processing code at the top of the page. For instance, use the if (isset($_POST['submit'])) code at the top of the page and do the query and all that from there, and have the form beneath all this code. This allows you to create an error message if anything goes wrong or the user made an error in what they entered. So then, if the user doesn't enter a username, you can create a variable called $error_username or whatever, and you can put "Please enter your username" in that variable and echo it next to the username text field. Make sense?
    can u show me an example of the username validation and where i can put it in the code, im sorry for all the questions btw, i really appreciate your help

  • #6
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    Sure. Using your code, this is more like what I'm talking about:

    PHP Code:
    <!--Register Form (Adding information) --> 
    <?php
    $error_stat 
    0;
    $message '';


    if (isset(
    $_POST['submit'])) {

    $username $_POST['username'];
    $password $_POST['password'];
    $md5password md5($_POST['password']);
    $forename $_POST['forename'];
    $surname $_POST['surname'];
    $email $_POST['email'];
    $mobile $_POST['mobile'];
    $dob $_POST['dob'];
    $location $_POST['location'];
    $ip $_SERVER['REMOTE_ADDR'];


    //Error checking

    if (empty($username)) {
       
    //Set the error_stat to 1, which means that an error has occurred
       
    $error_stat 1;
       
       
    //Set the message to tell the user to enter a username
       
    $message 'Please enter a username.';
    }


    //Then, only run the query if there were no errors (if $error_stat still equals 0)
    if ($error_stat == 0) {
       
    mysql_query("INSERT INTO users (username, password, forename, surname, email, mobile, dob, location, ipaddress) VALUES ('$username', '$md5password', '$forename', '$surname', '$email', '$mobile', '$dob', '$location', '$ip')");
       echo 
    "<h2>Registration Successful!</h2>";
       echo 
    "<p>Congratulations <b>$username</b>, your registration was successful. Your account has now been created with the following details:</p>";

       echo 
    "<p><b>Username:</b> $username</p>";
       echo 
    "<p><b>Password:</b> $password</p>";
       echo 
    "<p><b>Forename:</b> $forename</p>";
       echo 
    "<p><b>Surname:</b> $surname</p>";
       echo 
    "<p><b>Email:</b> $email</p>";
       echo 
    "<p><b>Mobile:</b> $mobile</p>";
       echo 
    "<p><b>DOB:</b> $dob</p>";
       echo 
    "<p><b>Location:</b> $location</p>";

       echo 
    "<p>You may now login.</p>";
    }


    //Then, for the form, only show it if 1) the form hasn't been submitted yet OR 2) there is an error
    if (!isset($_POST['submit']) || $error_stat == 1) {
    ?>


    <!--this will show whatever is in the $message variable -->
    <?=$message?>


    <form method="post" class="regform" action="">
    <fieldset>
    <label for="username">Username:</label>
    <input name="username" type="text" id="username" onkeyup="checkUsernameForLength(this);" />
    </fieldset>


    <fieldset>
    <label for="password">Password:</label>
    <input name="password" type="password" id="password1" onkeyup="checkUsernameForLength(this);" /> 
    </fieldset>

    <fieldset>
    <label for="forename">Forename:</label>
    <input name="forename" type="text" id="forename" onkeyup="checkUsernameForLength(this);" />
    </fieldset>

    <fieldset>
    <label for="surname">Surname:</label>
    <input name="surname" type="text" id="surname" onkeyup="checkUsernameForLength(this);" />
    </fieldset>

    <fieldset>
    <label for="email">Email:</label>
    <input name="email" type="text" id="email" onkeyup="checkUsernameForLength(this);" />
    </fieldset>

    <fieldset>
    <label for="mobile">Mobile:</label>
    <input name="mobile" type="text" id="mobile" onkeyup="checkUsernameForLength(this);" />
    </fieldset>

    <fieldset>
    <label for="dob">DOB:</label>
    <input name="dob" id="dob" onkeyup="checkUsernameForLength(this);" />
    </fieldset>

    <fieldset>
    <label for="location">Location:</label>
    <input name="location" type="text" id="location" onkeyup="checkUsernameForLength(this);" />
    </fieldset>
    <p></p>

    <p style="text-align: center">

    <input type="submit" class="signupbtn" name="submit" value="" />

    </p>

    </form>


    <?php
    }
    ?>
    Last edited by JohnDubya; 01-31-2008 at 06:34 PM.

  • #7
    Regular Coder
    Join Date
    Dec 2007
    Location
    Nebraska
    Posts
    113
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Normally, when you are putting user names into a database, you would also make certain that the user name entered didn't already exist. For that you would need to do a quick COUNT query to determine that.

    Also, none of the values going into the database are escaped, which is absolutely necessary. You will also likely find this registration form to be a spammer bot magnet. You really should take steps to prevent automated submissions.

    If there are other fields besides the username that are required and/or should be a specific format, they should be validated on the server side as well. Javascript validation is fine for the visitors that have Javascript enabled and aren't trying to break your application, but server side validation is the only secure kind of validation you can do for forms.
    Deliver yesterday, code today, think tomorrow.

  • #8
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Quote Originally Posted by JohnDubya View Post
    Sure. Using your code, this is more like what I'm talking about:

    PHP Code:
    <!--Register Form (Adding information) --> 
    <?php
    $error_stat 
    0;
    $message '';


    if (isset(
    $_POST['submit'])) {

    $username $_POST['username'];
    $password $_POST['password'];
    $md5password md5($_POST['password']);
    $forename $_POST['forename'];
    $surname $_POST['surname'];
    $email $_POST['email'];
    $mobile $_POST['mobile'];
    $dob $_POST['dob'];
    $location $_POST['location'];
    $ip $_SERVER['REMOTE_ADDR'];


    //Error checking

    if (empty($username)) {
       
    //Set the error_stat to 1, which means that an error has occurred
       
    $error_stat 1;
       
       
    //Set the message to tell the user to enter a username
       
    $message 'Please enter a username.';
    }


    //Then, only run the query if there were no errors (if $error_stat still equals 0)
    if ($error_stat == 0) {
       
    mysql_query("INSERT INTO users (username, password, forename, surname, email, mobile, dob, location, ipaddress) VALUES ('$username', '$md5password', '$forename', '$surname', '$email', '$mobile', '$dob', '$location', '$ip')");
       echo 
    "<h2>Registration Successful!</h2>";
       echo 
    "<p>Congratulations <b>$username</b>, your registration was successful. Your account has now been created with the following details:</p>";

       echo 
    "<p><b>Username:</b> $username</p>";
       echo 
    "<p><b>Password:</b> $password</p>";
       echo 
    "<p><b>Forename:</b> $forename</p>";
       echo 
    "<p><b>Surname:</b> $surname</p>";
       echo 
    "<p><b>Email:</b> $email</p>";
       echo 
    "<p><b>Mobile:</b> $mobile</p>";
       echo 
    "<p><b>DOB:</b> $dob</p>";
       echo 
    "<p><b>Location:</b> $location</p>";

       echo 
    "<p>You may now login.</p>";
    }


    //Then, for the form, only show it if 1) the form hasn't been submitted yet OR 2) there is an error
    if (!isset($_POST['submit']) || $error_stat == 1) {
    ?>


    <!--this will show whatever is in the $message variable -->
    <?=$message?>


    <form method="post" class="regform" action="">
    <fieldset>
    <label for="username">Username:</label>
    <input name="username" type="text" id="username" onkeyup="checkUsernameForLength(this);" />
    </fieldset>


    <fieldset>
    <label for="password">Password:</label>
    <input name="password" type="password" id="password1" onkeyup="checkUsernameForLength(this);" /> 
    </fieldset>

    <fieldset>
    <label for="forename">Forename:</label>
    <input name="forename" type="text" id="forename" onkeyup="checkUsernameForLength(this);" />
    </fieldset>

    <fieldset>
    <label for="surname">Surname:</label>
    <input name="surname" type="text" id="surname" onkeyup="checkUsernameForLength(this);" />
    </fieldset>

    <fieldset>
    <label for="email">Email:</label>
    <input name="email" type="text" id="email" onkeyup="checkUsernameForLength(this);" />
    </fieldset>

    <fieldset>
    <label for="mobile">Mobile:</label>
    <input name="mobile" type="text" id="mobile" onkeyup="checkUsernameForLength(this);" />
    </fieldset>

    <fieldset>
    <label for="dob">DOB:</label>
    <input name="dob" id="dob" onkeyup="checkUsernameForLength(this);" />
    </fieldset>

    <fieldset>
    <label for="location">Location:</label>
    <input name="location" type="text" id="location" onkeyup="checkUsernameForLength(this);" />
    </fieldset>
    <p></p>

    <p style="text-align: center">

    <input type="submit" class="signupbtn" name="submit" value="" />

    </p>

    </form>


    <?php
    }
    ?>

    when i run your code i get the following error:

    Parse error: syntax error, unexpected $end in C:\wamp\www\My Website\register2.php on line 114

    can u help me with this?

  • #9
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    Need to have another } after the lines:

    PHP Code:
       echo "<p>You may now login.</p>"

    I didn't test it.

  • #10
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    i cant get it to display the error message once the submit button is clicked, it doesnt show anything, but it doesnt allow the form to be submitted until the username is entered.

  • #11
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    You sure you got all the code? I just copied it and pasted it into a PHP page, and it worked great. Here is the page in its entirety:

    PHP Code:
    <!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>
    </head>

    <body>

    <?php 
    $error_stat 
    0
    $message ''


    if (isset(
    $_POST['submit'])) { 

        
    $username $_POST['username']; 
        
    $password $_POST['password']; 
        
    $md5password md5($_POST['password']); 
        
    $forename $_POST['forename']; 
        
    $surname $_POST['surname']; 
        
    $email $_POST['email']; 
        
    $mobile $_POST['mobile']; 
        
    $dob $_POST['dob']; 
        
    $location $_POST['location']; 
        
    $ip $_SERVER['REMOTE_ADDR']; 
        
        
        
    //Error checking 
        
        
    if (empty($username)) { 
            
    //Set the error_stat to 1, which means that an error has occurred 
            
    $error_stat 1
             
            
    //Set the message to tell the user to enter a username 
            
    $message 'Please enter a username.'
        } 
        
        
        
    //Then, only run the query if there were no errors (if $error_stat still equals 0) 
        
    if ($error_stat == 0) { 
            
    //mysql_query("INSERT INTO users (username, password, forename, surname, email, mobile, dob, location, ipaddress) VALUES ('$username', '$md5password', '$forename', '$surname', '$email', '$mobile', '$dob', '$location', '$ip')"); 
            
    echo "<h2>Registration Successful!</h2>"
            echo 
    "<p>Congratulations <b>$username</b>, your registration was successful. Your account has now been created with the following details:</p>"
        
            echo 
    "<p><b>Username:</b> $username</p>"
            echo 
    "<p><b>Password:</b> $password</p>"
            echo 
    "<p><b>Forename:</b> $forename</p>"
            echo 
    "<p><b>Surname:</b> $surname</p>"
            echo 
    "<p><b>Email:</b> $email</p>"
            echo 
    "<p><b>Mobile:</b> $mobile</p>"
            echo 
    "<p><b>DOB:</b> $dob</p>"
            echo 
    "<p><b>Location:</b> $location</p>"
        
            echo 
    "<p>You may now login.</p>";
        }
    }


    //Then, for the form, only show it if 1) the form hasn't been submitted yet OR 2) there is an error 
    if (!isset($_POST['submit']) || $error_stat == 1) { 
    ?> 


    <!--this will show whatever is in the $message variable --> 
    <?=$message?> 


    <form method="post" class="regform" action=""> 
    <fieldset> 
    <label for="username">Username:</label> 
    <input name="username" type="text" id="username" onkeyup="checkUsernameForLength(this);" /> 
    </fieldset> 


    <fieldset> 
    <label for="password">Password:</label> 
    <input name="password" type="password" id="password1" onkeyup="checkUsernameForLength(this);" />  
    </fieldset> 

    <fieldset> 
    <label for="forename">Forename:</label> 
    <input name="forename" type="text" id="forename" onkeyup="checkUsernameForLength(this);" /> 
    </fieldset> 

    <fieldset> 
    <label for="surname">Surname:</label> 
    <input name="surname" type="text" id="surname" onkeyup="checkUsernameForLength(this);" /> 
    </fieldset> 

    <fieldset> 
    <label for="email">Email:</label> 
    <input name="email" type="text" id="email" onkeyup="checkUsernameForLength(this);" /> 
    </fieldset> 

    <fieldset> 
    <label for="mobile">Mobile:</label> 
    <input name="mobile" type="text" id="mobile" onkeyup="checkUsernameForLength(this);" /> 
    </fieldset> 

    <fieldset> 
    <label for="dob">DOB:</label> 
    <input name="dob" id="dob" onkeyup="checkUsernameForLength(this);" /> 
    </fieldset> 

    <fieldset> 
    <label for="location">Location:</label> 
    <input name="location" type="text" id="location" onkeyup="checkUsernameForLength(this);" /> 
    </fieldset> 
    <p></p> 

    <p style="text-align: center"> 

    <input type="submit" class="signupbtn" name="submit" value="" /> 

    </p> 

    </form> 


    <?php 

    ?>

    </body>
    </html>

  • #12
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Quote Originally Posted by JohnDubya View Post
    You sure you got all the code? I just copied it and pasted it into a PHP page, and it worked great. Here it is again:
    I cant see the error message, it doesnt display anything.

    Here is my full code

    <?php
    session_start();
    include("database.php");
    include("login.php");
    ?>


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

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Jobs4U</title>


    <!--CSS Stylesheet -->
    <link rel="stylesheet" type="text/css" href="./css/main.css" />
    </head>

    </head>

    <body style="margin-left:1px;margin-right:1px;margin-bottom:0px;margin-top:0px;">

    <!--Main Container -->
    <div id="container">

    <!--Main Header -->
    <div id="header" style="width: 988px; height: 154px">
    &nbsp;
    </div>

    <!-- Navigation Menu -->
    <div id="nav_menu">
    <div class="link_container" style="width: 852px; height: 32px">
    <a href="page.htm" class="main_nav">home</a><span class="link_separator">::</span>
    <a href="page.htm" class="main_nav">products</a><span class="link_separator">::</span>
    <a href="page.htm" class="main_nav">services</a><span class="link_separator">::</span>
    <a href="page.htm" class="main_nav">about us</a><span class="link_separator">::</span>
    <a href="page.htm" class="main_nav">contact</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="text" name="search0" maxlength="30" size="30" >&nbsp;&nbsp;
    <a href="page.htm" class="main_nav">Quick Search&gt;&gt;&gt;</a></div>
    </div>




    <!-- Container (left) -->
    <div id="body">
    <div id="body-left-panel" style="width: 654px; height: 811px">
    <div style="padding:10px;">

    <!-- Left container 1(Top) -->
    <div class="box_upper_part">
    <img src="img/box_up-left_edge.png" alt="" class="align-left"/>
    <img src="img/box_up-right_edge.png" alt="" class="align-right"/>
    <span class="box_header">:: Registration</span></div>

    <!--(Middle) -->
    <div class="box_middle_part">
    <div class="pad_15tah">
    <hr class="hr_blue"/>
    </p>
    <table width="90&#37;" border="0" cellspacing="0" cellpadding="0" id="table6">
    <tr>
    <td height="24" align="left" valign="middle" bgcolor="#1B75A2">
    <img border="0" src="images/registerheader.gif" width="221" height="23"></td>
    </tr>
    <tr>
    <td align="left" valign="top" bgcolor="#FFFFFF">
    <img src="../../WN02/xhtml/images/speacer.gif" alt="" width="1" height="1" /></td>
    </tr>
    <tr>
    <td align="left" valign="top" class="bodybox1">
    <table width="100%" border="0" cellspacing="0" cellpadding="0" id="table7">
    <tr>
    <td align="left" valign="top">
    </td>
    </tr>
    </table>
    </td>
    </tr>
    </table>
    <div>

    </div>



    <!--Register Form -->
    <?php
    $error_stat = 0;
    $message = '';


    if (isset($_POST['submit'])) {

    $username = $_POST['username'];
    $password = $_POST['password'];
    $md5password = md5($_POST['password']);
    $forename = $_POST['forename'];
    $surname = $_POST['surname'];
    $email = $_POST['email'];
    $mobile = $_POST['mobile'];
    $dob = $_POST['dob'];
    $location = $_POST['location'];
    $ip = $_SERVER['REMOTE_ADDR'];


    //Error checking

    if (empty($username)) {
    //Set the error_stat to 1, which means that an error has occurred
    $error_stat = 1;

    //Set the message to tell the user to enter a username
    $message = 'Please enter a username.';
    }


    //Then, only run the query if there were no errors (if $error_stat still equals 0)
    if ($error_stat == 0) {
    mysql_query("INSERT INTO users (username, password, forename, surname, email, mobile, dob, location, ipaddress) VALUES ('$username', '$md5password', '$forename', '$surname', '$email', '$mobile', '$dob', '$location', '$ip')");
    echo "<h2>Registration Successful!</h2>";
    echo "<p>Congratulations <b>$username</b>, your registration was successful. Your account has now been created with the following details:</p>";

    echo "<p><b>Username:</b> $username</p>";
    echo "<p><b>Password:</b> $password</p>";
    echo "<p><b>Forename:</b> $forename</p>";
    echo "<p><b>Surname:</b> $surname</p>";
    echo "<p><b>Email:</b> $email</p>";
    echo "<p><b>Mobile:</b> $mobile</p>";
    echo "<p><b>DOB:</b> $dob</p>";
    echo "<p><b>Location:</b> $location</p>";

    echo "<p>You may now login.</p>";
    }
    }


    //Then, for the form, only show it if 1) the form hasn't been submitted yet OR 2) there is an error
    if (!isset($_POST['submit']) || $error_stat == 1) {
    ?>


    <!--this will show whatever is in the $message variable -->
    <?=$message?>

    <form method="post" action="">
    <fieldset>
    <label for="username">Username:</label>
    <input name="username" type="text" id="username" />
    </fieldset>


    <fieldset>
    <label for="password">Password:</label>
    <input name="password" type="password" id="password1" />
    </fieldset>

    <fieldset>
    <label for="forename">Forename:</label>
    <input name="forename" type="text" id="forename" />
    </fieldset>

    <fieldset>
    <label for="surname">Surname:</label>
    <input name="surname" type="text" id="surname" />
    </fieldset>

    <fieldset>
    <label for="email">Email:</label>
    <input name="email" type="text" id="email" />
    </fieldset>

    <fieldset>
    <label for="mobile">Mobile:</label>
    <input name="mobile" type="text" id="mobile" />
    </fieldset>

    <fieldset>
    <label for="dob">DOB:</label>
    <input name="dob" id="dob" />
    </fieldset>

    <fieldset>
    <label for="location">Location:</label>
    <input name="location" type="text" id="location" />
    </fieldset>
    <p></p>

    <p style="text-align: center">

    <input type="submit" class="signupbtn" name="submit" value="" />

    </p>

    </form>


    <?php
    }
    ?>
    </div>
    </div>




    <!-- (bottom) -->
    <div class="box_lower_part">
    <img src="img/box_low-left_edge.png" alt="" class="align-left"/>
    <img src="img/box_low-right_edge.png" alt="" class="align-right"/>
    </div>

    <!--Left Container 2 (Top) -->
    </div>
    </div>
    <p>


    <!--Right Panel 1 -->
    <div id="body-right-panel" style="width: 333px; height: 811px">
    <div style="padding:10px;">


    <!--Top -->
    <div class="box_upper_part">
    <img src="img/box_up-left_edge_dark.png" alt="" class="align-left"/>
    <img src="img/box_up-right_edge_dark.png" alt="" class="align-right"/>
    <span class="box_header">Member Login</span></div>


    <!--Middle -->
    <div class="box_middle_part">
    <div style="padding:5px;">
    <ul class="img_red">


    <!--Login -->
    <?php displayLogin(); ?>
    <p><br /></p>
    </div>
    </div>


    <!-- (bottom) -->
    <div class="box_lower_part">
    <img src="img/box_low-left_edge_dark.png" alt="" class="align-left"/>
    <img src="img/box_low-right_edge_dark.png" alt="" class="align-right"/>
    </div>


    <!--Right Panel 2 (top) -->
    <div class="box_upper_part" style="width: 313px; height: 29px">
    <img src="img/box_up-left_edge_dark_nodot.png" alt="" class="align-left"/>
    <img src="img/box_up-right_edge_dark.png" alt="" class="align-right"/>
    <span class="box_header">:: Resource Centre</span>
    </div>


    <!--Middle -->
    <div class="box_middle_part">
    <div style="padding:10px;">
    <div align="center">
    <table border="0" width="65%" id="table12" cellspacing="0" cellpadding="0" bgcolor="#E7E6E4">
    <tr>
    <td>
    <img border="0" src="images/cv.gif" width="221" height="23"></td>
    </tr>
    <tr>
    <td><p>&nbsp;</p>
    <p><span class="navyboldtxt">CV Advice</span>. </p>
    <p>&nbsp;- Advice on how to create a CV </p>
    <p>&nbsp;- Create your own CV</p>
    <p>&nbsp;</p>
    </td>
    </tr>
    </table>
    <table border="0" width="65%" id="table13" cellspacing="0" cellpadding="0" bgcolor="#E7E6E4">
    <tr>
    <td>
    <img border="0" src="images/interview.gif" width="221" height="23">
    </td>
    </tr>
    <tr>
    <td>&nbsp;<p><span class="navyboldtxt">
    Interview Advice</span>. </p>
    <p>&nbsp;-&nbsp; How to approach an interview </p>
    <p>&nbsp;- Interview Techniques</p><p>&nbsp;</td>
    </tr>
    </table>
    </div>
    </div>
    </div>


    <!-- (bottom) -->
    <div class="box_lower_part" style="width: 313px; height: 22px">
    <img src="img/box_low-left_edge_dark.png" alt="" class="align-left"/>
    <img src="img/box_low-right_edge_dark.png" alt="" class="align-right"/>
    </div>
    </div>
    </div>
    </div>


    <!-- Footer -->
    <div id="footer">
    <div id="footer_designby" style="width: 987px; height: 30px">
    <div class="designedby">Designed by
    <a href="http://">Philip Rodgers</a></div>
    </div>
    </div>
    </div>

    </body>
    </html>
    Last edited by PRodgers4284; 01-31-2008 at 09:40 PM.

  • #13
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    I just copied and pasted all that code into a page, and the error message showed up next to the username text field, like you wanted.

  • #14
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Quote Originally Posted by JohnDubya View Post
    I just copied and pasted all that code into a page, and the error message showed up next to the username text field, like you wanted.
    I got it working, i had to add the <?php echo "$usernameerror";?> in the form beside the username field.
    Last edited by PRodgers4284; 02-01-2008 at 11:53 AM.

  • #15
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    I would like to put a function in to check the username against the database i have, i was wondering if u could help me add the function in to the script, to display an error if the username is already in use, the function I have is below, i dont know where to add it in to the code, please help.

    function usernameTaken($username){
    global $conn;
    if(!get_magic_quotes_gpc()){
    $username = addslashes($username);
    }
    $q = "select username from users where username = '$username'";
    $result = mysql_query($q,$conn);
    return (mysql_numrows($result) > 0);
    }
    Last edited by PRodgers4284; 02-01-2008 at 11:51 AM.


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •