I'm aware that this question has been asked a huge number of times but the answers seem really specific to the script posted and with my current knowledge I can't make the transition from the corrected script to implementing it into my own.
This all works fine - it submits on the same page and provides feedback of any errors, but I want any errors to be echoed beneath the form making it more convenient for the user to just change what they entered incorrectly and re-submit.
From what I can gather from the questions I read before posting this - it may only be possible using jQuery/Ajax?
My script:
<?php
if(isset($_POST['submit'])){
require "connection.php";
$username = $_POST["username"];
$password = $_POST["password"];
$email = $_POST["email"];
$repeat_password = $_POST["repeat_password"];
$username_query = mysql_query("SELECT id FROM users WHERE username = '$username'");
$email_query = mysql_query("SELECT id FROM users WHERE email = '$email'");
if($username == "" || $password == "" || $repeat_password == "" || $email == ""){
die("All boxes must be filled out!");
}// ^ Checking if all boxes have been filled out
else {
if (!ctype_alnum($username)){
die("Username can only contain letters and numbers");
}// ^ Checking if username is alphanumeric
else {
if (strlen($username) < 6 || strlen($username) > 15){
die("Username must be between 6-15 characters.");
}// ^ Checking if username is between 6-15 characters
else {
if (mysql_num_rows($username_query) != 0){
die("Username is taken, please choose another.");
}// ^ Checking if username exists in database
else {
if (!preg_match("/[0-9]/",$password)){
echo "password doesnt contain a number";
}
else {
if ($password != $repeat_password){
die("Passwords do not match");
}// ^ Checking if password and repeat_password match
else {
if (strlen($password) < 6){
die("Password must be atleast 6 characters long.");
}// ^ Checking if password is longer than 6 characters
else {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
die("E-mail address is not vaild.");
}// ^ Checking if e-mail address is valid
else {
if (mysql_num_rows($email_query) != 0){
die("This e-mail address has already been used to create a different account.");
}// ^ Checking if e-mail address has already been used
else {
mysql_query("INSERT INTO users (username, password, email, signup_date) VALUES ('$username', '$password', '$email', CURDATE())") or die(mysql_error());
echo "Account succesfully created, welcome ".$username."!";
}
}
}
}
}
}
}
}
}
exit;
}
//
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table>
<tr>
<td>
Username:
</td>
<td>
<input type="text" name="username">
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td>
Repeat password:
</td>
<td>
<input type="password" name="repeat_password">
</td>
</tr>
<tr>
<td>
E-mail:
</td>
<td>
<input type="email" name="email">
</td>
</tr>
<tr>
<td colspan="2">
<center><input type="submit" name="submit"></center>
</td>
</tr>
</table>
</form>