Jump to content

Form Validation


lukep11a

Recommended Posts

Hi, I am putting together a registration form for a new site, it correctly inserts all the data into the table and sends a confirmation email to the users email address.

Where I am struggling is with the validation code. I have validation code that works to check the email address and the password are correct, they are both separated into separate functions. For the other fields all I want to do is basically to check to make sure they have not been left empty. I have code that works for this also but I am not sure how to put this together.

 

This is the code that currently submits all the data to a table called users, it checks to make sure the email and password are valid and that the user doesn't already exist:

 

<?php

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

if (registerNewUser($_POST['first_name'], $_POST['last_name'], $_POST['gender'], $_POST['location'], $_POST['password'], $_POST['password2'], $_POST['email'], $_POST['email2'])){

echo "<p class='normal'>Thank you for registering, a confirmation email has been sent to your inbox. You may now login to your account page and submit a team.</p>";

}else {

echo "<p class='fail'>Registration failed! Please try again. Make sure your passwords match and you have provided a valid email address.</p>";
show_registration_form();

}

} else {
// has not pressed the register button
show_registration_form();	
}

function registerNewUser($first_name, $last_name, $gender, $location, $password, $password2, $email, $email2)
{

    global $seed;

    if (!valid_password($password) || !valid_email($email) || $password != $password2 || user_exists($email))
    {
        return false;
    }

$sql = sprintf("insert into users (first_name,last_name,gender,location,password,email) value ('%s','%s','%s','%s','%s','%s')",
        mysql_real_escape_string($first_name), mysql_real_escape_string($last_name), mysql_real_escape_string($gender), mysql_real_escape_string($location), mysql_real_escape_string(sha1($password . $seed)), mysql_real_escape_string($email));

if (mysql_query($sql))
    {
        $id = mysql_insert_id();

        if (sendActivationEmail($password, $id, $email))
        {
		return true;
        } else
        {
            return false;
        }

    } else
    {
        return false;
    }
    return false;
}
?>

 

This is the code that I have been testing to check to make sure all fields have data entered:

 

<?php
/* Check all form inputs using check_input function */
$first_name		= check_input($_POST['first_name'], "Enter your first name");
$last_name		= check_input($_POST['last_name'], "Enter your last name");
$email			= check_input($_POST['email']);
$email2			= check_input($_POST['email2']);
$password		= check_input($_POST['password'], "Please enter your password");
$password2		= check_input($_POST['password2'], "Please confirm your password");

/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?>

 

Both sets of code work fine on their own but I am not sure how to bring them together and display the relevant error message. If anyone can help me with this I would really appreciate it.

Link to comment
Share on other sites

php only generates the HTML page for you which means that any form you fill in will have to be validated and then a new page displayed (with the error messages on it) and they will have to submit the form again after they have correctly filled in the form the second time.

 

Two ways of doing it. either do it all in one php program, or load a validation php program which does the validation and then shows the error messages and then reloads the form for them to have another go.

 

Why dont you play around with a php program and html form and just validate one text input box and see what you need to do. Then stick in your code once you have decided how you will do it.

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.