0

I am currently creating a registration page and checking for errors. I have the errors working correctly but when I report them, they end up on a new page. I was wondering how I get it to not redirect to a different page?

I echo out a div that I was thinking would appear next to the form but that is not occuring. Here is my form and error validation:

<form action="signup.php" method="post">
                <input type="text" name="First_name" placeholder="First Name" />
                <input type="text" name="Last_name" placeholder="Last Name" />
                <input type="text" name="Email" placeholder="Email Address" />
                <input type="text" name="Email2" placeholder="Comfirm Email Address" />
                <input type="password" name="Password" placeholder="Password" />
                <input type="submit" value="Submit" name="submit"/>
</form>

This is part of my php file.

if (isset($_POST['submit'])) {
$error = array();
$firstname=$_POST['First_name'];
$lastname=$_POST['Last_name'];
$email=$_POST['Email'];
$email2=$_POST['Email2'];
$password=$_POST['Password'];

if (strlen($password) <= 6 and strlen($password) >= 1) {
    if (strlen($password) >= 20) {
        if (strlen($email) >= 1 and strlen($email) <= 55) {
            if ($email == $email2) {
                if (ereg('^[a-zA-Z0-9\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $email)) {
                        $sql = "INSERT INTO Users (First_Name, Last_Name, Email, Password) VALUES ('$firstname','$lastname','$email','$password')";
                            echo "Regiration Complete. Check Email for Validation.";
                            exit;
                    } else {
                        $error[] = 'Email is wrong';
                    }
                } else {
                    $error[] = 'Email addresses are not the same';
                }
            } else {
                $error[] = 'Email Address is too long';
            }
        } else {
            $error[] = 'Password is too long';
        }
    } else {
        $error[] = 'Password must be 6 Characters';
    }
}
echo '<div class="errormsgbox"> <ol>';
    foreach ($error as $key => $values) {
        echo '  <li>'.$values.'</li>';
    }
echo '</ol></div>';
user081608
  • 1,093
  • 3
  • 22
  • 48
  • Is your form html on the same page as your php code? Or are they on different pages? If they are, you need to move the html form into signup.php. – Martyn Shutt Jul 19 '15 at 16:16
  • They are on different pages. I have an index.html and a registration.php. So I would need to combine them both into an index.php or just move the form into the php file? – user081608 Jul 19 '15 at 16:19
  • I've now provided an answer to your question. – Martyn Shutt Jul 19 '15 at 16:22

3 Answers3

2

If you want to include your form on the same page, then you need to include it in your main signup.php file, like so;

<!--your html form goes here-->

<?php
  //all php code goes in here

?>

Anything outside of the PHP opening and closing tags in a .php file will be treated as HTML markup.

Martyn Shutt
  • 1,671
  • 18
  • 25
  • Is it bad practice to write the php in the same file? – user081608 Jul 19 '15 at 16:24
  • That's all a matter of opinion. I wouldn't say so for small projects, but for large projects it is a good idea to separate your "business logic from presentation logic". – Martyn Shutt Jul 19 '15 at 16:29
  • I understand. Is there a way separate this from each other and still get the same output or no? – user081608 Jul 19 '15 at 16:30
  • @user081608 Yes, there are numerous ways, but you'd still need to echo out the error messages next to your form, so at a minimum, you would still need to echo those on your form page. I'd suggest looking at Model View Controller patterns in PHP, or storing the errors in a session variable and echoing those out on your form page for a simpler approach. – Martyn Shutt Jul 19 '15 at 16:35
1

i recommend you to look at this plugin: http://rickharrison.github.io/validate.js/

in other ways with easy jquery or js its possible without that the page is loading new..

but you MUST make an second checkup via php because people can easy manipulate the clientsided js code ... good luck :)

edit1: you can always work with ajax to sendd and validate the data in php if you want the elegant way (ajax also client sided..) :)

just read and learn

ProJaCore
  • 42
  • 8
1

You can use HTML5 to validate these Data without PHP - so the form doesn't even get sent if its violating your standards.

You can read more about it here:

http://www.the-art-of-web.com/html/html5-form-validation/

To your question:

You load a different page. This means, that you have to include the form there, too, if you want to get it shown there. Otherwise it won't display.

SophieXLove64
  • 316
  • 1
  • 11
  • Thats very interesting. The only limitations I see are the minimum length and checking to see if the two emails are the same. Is that possible with is? – user081608 Jul 19 '15 at 16:53
  • Unfortunatly HTML5 cannot check if two Input-Fields are the same - but a little JavaScript could do. But length validation is possible very easy: http://stackoverflow.com/questions/10281962/is-there-a-minlength-validation-attribute-in-html5 – SophieXLove64 Jul 19 '15 at 16:56
  • So could I check that before I send it to the php and submit it to the database? – user081608 Jul 19 '15 at 17:20
  • 1
    I'd still use PHP to double validate. I guess there is a workaround to cheat HTML5. But you can check if the Emails are equal with JavaScript before, yes. – SophieXLove64 Jul 19 '15 at 20:44