Rather than make a new thread, currently trying to figure out why this won't work.
<?php require_once 'connection.php'; ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" href="css/mystyle.css">
<title>Game of Thrones social</title>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script>
$(document).ready(function() {
$("form").on("submit", function(event) {
event.preventDefault();
$("span.error").empty()
$.getJSON('registerForm.php', $(this).serialize(), function(data) {
if (!data.errors) {
alert(data.message) // deal with a no-error response ( all is good)
}else{
$.each(data.errors,function(i,datum){
$("[name='"+datum.name+"']").next().html(datum.error)
})
}
});
});
});
</script>
</head>
<body>
<form action="" method="GET">
<div class="formControl">
<input type="input" name="username" placeholder="Username" value="<?php echo isset($_POST['username']) ? $_POST['username'] : '' ?>">
<span class="error"> </span>
</div>
<div class="formControl">
<input type="text" name="email" placeholder="E-mail" value="<?php echo isset($_POST['email']) ? $_POST['email'] : '' ?>">
<span class="error"></span>
</div>
<div class="formControl">
<input type="password" name="password" placeholder="Password">
<span class="error"> </span>
</div>
<div class="formControl">
<input type="password" name="repeatPassword" placeholder="Repeat password">
<span class="error"> </span>
</div>
<div class="formControl">
<input type="hidden" name="code" value="<?php echo substr(str_shuffle("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 1).substr(md5(time()),1); ?>">
<span class="error"> </span>
</div>
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
require_once'connection.php';
header('Content-Type: application/json');
$errors = [];
$username = trim($_GET['username']);
$email = trim($_GET['email']);
$password = trim($_GET['password']);
$repeatPassword = trim($_GET['repeatPassword']);
$code = $_GET['code'];
$query = $db->prepare("SELECT username.username FROM username WHERE username.username = :username LIMIT 1");
$query->bindValue(':username', $username, PDO::PARAM_STR);
$query->execute();
if ( $query->rowCount() > 0 ) {
$response=1;
$errors[]= ["name"=>"username","error"=>"Username taken"];
}
if(filter_var($username, FILTER_VALIDATE_REGEXP,["options"=> [ "regexp" => "/.{3,25}/"]]) === FALSE){
$errors[]= ["name"=>"username","error"=>"invalid Id (3 to 25 characters)"];
}
if(preg_match('/[^a-z_\-0-9]/i', $username))
{
$errors[]= ["name"=>"username","error"=>"invalid Id (Usernames may not contain symbols)"];
}
if(filter_var($email,FILTER_VALIDATE_EMAIL) === FALSE) {
$errors[]= ["name"=>"email","error"=>"invalid Email"];
}
$emailQ = $db->prepare("SELECT username.eMail FROM username WHERE username.eMail = :email LIMIT 1");
$emailQ->bindValue(':email', $email, PDO::PARAM_STR);
$emailQ->execute();
if ( $query->rowCount() > 0 ) {
$response=1;
$errors[]= ["name"=>"email","error"=>"Email registered"];
}
if(filter_var($password, FILTER_VALIDATE_REGEXP,["options"=> [ "regexp" => "/.{6,25}/"]]) === FALSE){
$errors[]= ["name"=>"password","error"=>"invalid password (6 to 25 characters)"];
}
if(!preg_match("/(?=[a-z]*[0-9])(?=[0-9]*[a-z])([a-z0-9-]+)/i",$password)) {
$errors[]= ["name"=>"password","error"=>"Password must contain numbers and letters"];
}
if($password !== $repeatPassword){
$errors[]= ["name"=>"repeatPassword","error"=>"passwords don't match"];
}
$salt= uniqid(mt_rand(), true);
$options=['salt'=>$salt, 'cost'=>12];
if (count($errors) === 0) {
// everything is OK, the browser should send us to the next page
echo json_encode(["message"=>"Please view your emails to activate your account"]);
$sql = "INSERT INTO username (username,password, eMail ,joinedDate, active, activecode) VALUES (:username, :password, :email ,NOW(), 0, :code)";
$query = $db->prepare($sql);
$query->execute(array(
':username'=> $username,
':password'=> $cryptpwd=crypt($password,'$2y$12$'.$salt.'$'),
':email'=> $email,
':code'=> $code
));
echo $message = '
http://gotsocial.co.uk/active.php?activecode='.$code.'.
';
$to = $email;
$subject = 'Game of Thrones Social';
$from = "register@gotsocial.co.uk";
$result = mail($to, $subject, $message, "From: $from");
}
echo json_encode($errors);
When it fails validation it gives me an undefined alert rather than falling pack on the server side validation
Currently it's running here:
http://gotsocial.co.uk/register.php