Reputation: 1123
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>';
Upvotes: 0
Views: 2225
Reputation: 316
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.
Upvotes: 1
Reputation: 42
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
Upvotes: 1
Reputation: 1706
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.
Upvotes: 2