RightLeftRight12
RightLeftRight12

Reputation: 99

Registration page. Printing out user errors in php

I've created a registration page whch checks for errors and prints them out for the user. For example if they do not have a valid email address it says invalid email address. At the top where the logic is done it lets the webpage know if there are any errors so later I can register them without errors. In the bottom where the html it I am trying to printout errors so they show next to the text field. I have created some errors but for some reason they print before the user even prints anything. The code is as follows:

<form action="" method="POST">
                    <input type="hidden" name="action" value="register" />
                    <?php if(@$_POST['action'] == 'register') {
                    }
                    <input type="text" name="firstname" maxlength = "16" class="text-bars" placeholder="Firstname"/>
                    <input type="text" name="lastname" maxlength = "16" class="text-bars" placeholder="Lastname"/><br /><br />
                    <input type="text" name="username" maxlength = "16" class="text-bars" placeholder="Username" />
                    <?php if(user_exists($_POST['username']) === true){
                            echo "Username already exsits.";
                            }else if(preg_match("/\\s/", $_POST['username']) == true){
                            echo 'Your username must not contain spaces.';
                            }
                    ?>
                    <br /><br />
                    <input type="password" name="password" maxlength = "16" class="text-bars" placeholder="Password" />
                    <input type="password" name="re_password" maxlength = "16" class="text-bars" placeholder="Re-Enter Password" />

                            if($_POST['password'] !== $_POST['re_password']){
                            echo "Your passwords do not match.";
                            }else if(strlen($_POST['password']) !== 0 && strlen($_POST['password']) < 6){
                            echo "Your password must be at least 6 characters.";
                            } 
                    ?>
                    <br /><br />
                    <input type="text" name="email" maxlength = "25" class="text-bars" placeholder="Email" />
                    <input type="text" name="re_email" maxlength = "25" class="text-bars" placeholder="Re-enter Email" />
                    <?php
                        if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false && $_POST['email'] !== 0){
                            echo "A valid email address is required.";
                        }else if(email_exists($_POST['email']) === true){
                            echo "Email address already in use.";
                        } else if($_POST['email'] !== $_POST['re_email']){
                            echo "Your emails do not match.";
                        }
                    ?>
                    <br /><br />
                    <input type="text" name="zipcode" maxlength = "7" class="text-bars" placeholder="Zip Code" /><br /><br />

                    <input type="submit" value="Submit" />
          </form>

The errors that checks for a password is above 6 characters long and email validate seem to print right when the page loads. Any solutions to this problem?

Thanks everyone!

$errors = array();

if(empty($_POST) === false){
$required_fields = array('firstname', 'lastname', 'username', 'password', 're_password', 'email', 're_email', 'zipcode');
foreach($_POST as $key=>$value){
    if(empty($value) && in_array($key, $required_fields) === true){
        $errors[] = 'All fields are required';
        break 1;
    }
}

if(empty($errors) === true){
    if(user_exists($_POST['username']) === true){
        $errors[] = 'Username already exsits.';
        }
    if(preg_match("/\\s/", $_POST['username']) == true){
        $errors[] = 'Your username must not contain spaces.';
    }
    if(strlen($_POST['password']) < 6){
        $errors[] = 'Your password must be at least 6 characters.';
    }
    if($_POST['password'] !== $_POST['re_password']){
        $errors[] = 'Your passwords do not match.';
                            }
    if(strlen($_POST['password']) !== 0 && strlen($_POST['password']) < 6){
        $errors[] = 'Your password must be at least 6 characters.';
                            }
    if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
        $errors[]= 'A valid email address is required.';
    }
    if(email_exists($_POST['email']) === true){
        $errors[] = 'Email address already in use.';
    } 
    if($_POST['email'] !== $_POST['re_email']){
        $errors[] = 'Your emails do not match.';
    }
}

}

Upvotes: 0

Views: 1840

Answers (4)

user2341406
user2341406

Reputation:

Add an input hidden to your code inside the <form> element.

<input type="hidden" name="action" value="register" />

Around your php code, use the following

<?php if(@$_POST['action'] == 'register') {

} ?>

This will trigger your php code only when the form is submitted by the user.

This is the exact code you need

<?php
    if(@$_POST['action'] == 'register') {
        if(user_exists($_POST['username']) === true){
            $errorUser = "Username already exsits.";
        }else if(preg_match("/\\s/", $_POST['username']) == true){
            $errorUser = 'Your username must not contain spaces.';
        }

        if($_POST['password'] !== $_POST['re_password']){
            $errorPassword = "Your passwords do not match.";
        }else if(strlen($_POST['password']) !== 0 && strlen($_POST['password']) < 6){
            $errorPassword = "Your password must be at least 6 characters.";
        }

        if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false && $_POST['email'] !== 0){
            $errorEmail = "A valid email address is required.";
        }else if(email_exists($_POST['email']) === true){
            $errorEmail = "Email address already in use.";
        } else if($_POST['email'] !== $_POST['re_email']){
            $errorEmail = "Your emails do not match.";
        }
    }
?>

<form action="" method="POST">
    <input type="hidden" name="action" value="register" />
    <input type="text" name="firstname" maxlength = "16" class="text-bars" placeholder="Firstname"/>
    <input type="text" name="lastname" maxlength = "16" class="text-bars" placeholder="Lastname"/>
    <?=@$errorUser;?>
    <br /><br />
    <input type="text" name="username" maxlength = "16" class="text-bars" placeholder="Username" />
    <br /><br />
    <input type="password" name="password" maxlength = "16" class="text-bars" placeholder="Password" />
    <input type="password" name="re_password" maxlength = "16" class="text-bars" placeholder="Re-Enter Password" />
    <?=@$errorPassword;?>
    <br /><br />
    <input type="text" name="email" maxlength = "25" class="text-bars" placeholder="Email" />
    <input type="text" name="re_email" maxlength = "25" class="text-bars" placeholder="Re-enter Email" />
    <?@$errorEmail;?>
    <br /><br />
    <input type="text" name="zipcode" maxlength = "7" class="text-bars" placeholder="Zip Code" /><br /><br />
    <input type="submit" value="Submit" />
</form>

Upvotes: 1

michi
michi

Reputation: 6625

I suggest you validate user input in a separate block of code after submit, e.g. at the top of your script:

if (isset($_POST("submit")) {  // form has ben submitted...

    // validate
    if (!isset($_POST['firstname']) || strlen($_POST['firstname']) == 0)
        $error['firstname'] = "Please enter a first name!";

    // do this with all your fields

    if (!isset($error)) { // no error = all fields valid

        // save fields to database
        // route to another page
        exit();
    } // if !isset $error

} // if isset

in your form, you...

<form name="myform" method="post">
<?php=(isset($error)?'There were errors:':'')?>
<?php $fieldname = 'firstname'; ?>
<?php=(isset($error[$fieldname])?$error[$fieldname].'<br />':'')?>
<input type="text" name="firstname" maxlength="16" 
    class="text-bars<?php=(isset($error[$fieldname])?' error':'')?>" 
    value="<?php=(isset($_POST[$fieldname])?$_POST[$fieldname]:'')?>"/>

<!-- do the same for all other fields... -->

<input type="submit" name="submit" value="submit" /> <!-- add name attribute! -->
</form>

Like this, you are displaying an error-message...
- on top of the form if $error is set
- on top of the field that contains an error if $error[fieldname] is set...
- the class attribute of the field is set to text-bars error, so if you define a class error, you can highlight the field.

Upvotes: 0

VCNinc
VCNinc

Reputation: 757

First, declare errors

$errors = array();

Everytime you catch an error, append it to the variable

$errors[] = 'Your Error Text';

Finally, print the errors

echo '<ul>';
foreach($errors as $error) {
    echo "<li>$error</li>";
}
echo '</ul>';

Upvotes: 0

RMcLeod
RMcLeod

Reputation: 2581

Your problem is that the validation gets done whether or not the form has been submitted. I would recommend doing all of your validation in PHP before your html, you can use an array to capture errors and print them out in the form. This will keep your logic and content separated to a greater degree which makes debugging easier.

<?php
if ($_POST) { //Check that the form has actually been submitted
    $errors = array();
    if(user_exists($_POST['username']) === true) {
        $errors['user'] == 'Username exists';
    }
    ... // more error checking
}

... //form output
<input type="text" name="username" maxlength = "16" class="text-bars" placeholder="Username" />
<?php print(isset($errors['username'])) ? $errors['username'] : ''; ?>
... //more form output

That should get you started

Upvotes: 0

Related Questions