I am creating a simple registration form page(register.html): First Name, Last Name, Email, Username, Password, Confirm Password. I have it set to validate the input client side in JavaScript and if that passes it goes to register.php which validates the data on the back end before making it safe and adding it to the database. All of that is working fine.
However, there is a problem if say for example the validation on the back end fails. I don't want the client to lose all the information they entered and have to do it all again, but since it already loaded register.php all that form data is now sitting in the PHP script but on a new page. And I am unsure how to deal with this problem. I could create a duplicate form on the register.php page and use the PHP script to refill the form if it fails on the back end. But if I am going to do that I might as well just put the PHP code into my register.html and run it all that way. But when I change action="register.html" to action="" and put my PHP code in there, nothing happens. I've searched around and looked into isset but that doesn't seem to run either, the HTML page just reloads and clears all the fields, and I don't even see any echoes or anything.
<!DOCTYPE HTML>
<html>
<head>
<title>Family Beacon Registration</title>
<style>
.required {color: #FF0000;}
.error {color: #FF0000;}
</style>
<script type="text/javascript">
function validate()
{
var hasErrors = false;
var first_name = document.forms["registration_form"]["first_name"].value;
if(first_name == "")
{
document.getElementById("first_name_error").innerHTML = "* First Name is a required field";
hasErrors = true;
}
else
document.getElementById("first_name_error").innerHTML = "*";
var last_name = document.forms["registration_form"]["last_name"].value;
if(last_name == "")
{
document.getElementById("last_name_error").innerHTML = "* Last Name is a required field";
hasErrors = true;
}
else
document.getElementById("last_name_error").innerHTML = "*";
var email = document.forms["registration_form"]["email"].value;
if(email == "")
{
document.getElementById("email_error").innerHTML = "* Email is a required field";
hasErrors = true;
}
else if(email.length > 40)
{
document.getElementById("email_error").innerHTML = "* Email must be less than 40 characters in length.";
hasErrors = true;
}
else if(email.indexOf("@") == -1)
{
document.getElementById("email_error").innerHTML = "* A valid e-mail address is required";
hasErrors = true;
}
else
document.getElementById("email_error").innerHTML = "*";
var username = document.forms["registration_form"]["username"].value;
if(username == "")
{
document.getElementById("username_error").innerHTML = "* Username is a required field";
hasErrors = true;
}
else if(username.length < 3 || username.length > 20)
{
document.getElementById("username_error").innerHTML = "* Username must be between 3 and 20 characters in length";
hasErrors = true;
}
else
document.getElementById("username_error").innerHTML = "*";
var password = document.forms["registration_form"]["password"].value;
if(password == "")
{
document.getElementById("password_error").innerHTML = "* Password is a required field";
hasErrors = true;
}
else if(password.length < 8 || password.length > 20)
{
document.getElementById("password_error").innerHTML = "* Passwords must be between 8 and 20 characters in length";
hasErrors = true;
}
else
document.getElementById("password_error").innerHTML = "*";
var confirm_password = document.forms["registration_form"]["confirm_password"].value;
if(confirm_password == "")
{
document.getElementById("confirm_password_error").innerHTML = "* Confirm Password is a required field";
hasErrors = true;
}
else if(confirm_password != password)
{
document.getElementById("confirm_password_error").innerHTML = "* Passwords do not match";
document.getElementById("password_error").innerHTML = "* Passwords do not match";
hasErrors = true;
}
else
document.getElementById("confirm_password_error").innerHTML = "*";
return !hasErrors;
}
</script>
</head>
<body>
<h2>Family Beacon Registration</h2>
<p><span class="required" id="required">* required field</span></p>
<form name="registration_form" action="register.php" onsubmit="return validate();" method="post">
First Name:<input type="text" name="first_name" id="first_name" />
<span class="error" id="first_name_error">* </span><br><br>
Last Name:<input type="text" name="last_name" id="last_name" />
<span class="error" id="last_name_error">* </span><br><br>
E-Mail:<input type="text" name="email" id="email" />
<span class="error" id="email_error">* </span><br><br>
Username:<input type="text" name="username" id="username" />
<span class="error" id="username_error">* </span><br><br>
Password:<input type="password" name="password" id="password" />
<span class="error" id="password_error">* </span><br><br>
Confirm Password:<input type="password" name="confirm_password" id="confirm_password" />
<span class="error" id="confirm_password_error">* </span><br><br>
<input type="reset" name="reset" value="Reset" />
<input type="submit" name="submit" value="Register" />
</form>
</body>
</html>