So I have two files my first one is the form itself
Here's the form index.php
<!DOCTYPE html>
<html>
<body>
<form method="post" action="http://localhost/s/r.php" >
Name: <input type="text" name="name"><br>
Username: <input type="text" name="username"><br>
Email: <input type="text" name="email"><br>
Password: <input type="text" name="pass1"><br>
Password, again: <input type="text" name="pass2"><br>
<input type="submit">
</form>
</body>
</html>
And then my r.php
<?php
include 'db.php';
$name = mysqli_real_escape_string($con, $_POST['name']);
$username = mysqli_real_escape_string($con, $_POST['username']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$pass1 = mysqli_real_escape_string($con, $_POST['pass1']);
$pass2 = mysqli_real_escape_string($con, $_POST['pass2']);
// Verification
if (empty($name) || empty($username) || empty($email) || empty($pass1) || empty($pass2))
{
echo "Complete all fields";
// you can stop it here instead of putting the curly brace ALL the way at the bottom :)
return;
}
// Password match
if ($pass1 <> $pass2)
{
echo $passmatch = "Passwords don't match";
}
// Email validation
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
echo $emailvalid = "Enter a valid email";
}
// Password length
if (strlen($pass1) <= 6)
{
echo $passlength = "Password must be at least 6 characters long";
}
// Password numbers
if (!preg_match("#[0-9]+#", $pass1))
{
echo $passnum = "Password must include at least one number!";
}
// Password letters
if (!preg_match("#[a-zA-Z]+#", $pass1))
{
echo $passletter = "Password must include at least one letter!";
}
?>
And I also have my db.php
which isn't relevant to the issue. So I'm trying to make the form not go to r.php
and display the errors if there is an error, but rather make it display next to the form, in index.php
. Is there a way to prevent it from going to r.php
or would I have to combine the two scripts?