I'm very new to JSON, currently I have been trying to insert into a database currently I get the correct message for everything input an passing validation correctly (the validation still needs some work I know) however I can't get it to insert into a database, any advice would be great.
HTML Game of Thrones social
<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()
$("span.success").empty()
$.getJSON('registerForm.php', $(this).serialize(), function(data) {
if (!data.errors) {
$(".success").append(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>
<span class="success"></span>
<form action="" method="POST">
<div class="formControl">
<input type="input" name="username" placeholder="Username" value="">
<span class="error"> </span>
</div>
<div class="formControl">
<input type="text" name="email" placeholder="E-mail" value="">
<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="Confirm 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
<?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']);
$errors = [];
if(filter_var($username, FILTER_VALIDATE_REGEXP,["options"=> [ "regexp" => "/.{6,25}/"]]) === FALSE){
$errors[]= ["name"=>"username","error"=>"invalid Id (6 to 25 characters)"];
}
if(filter_var($email,FILTER_VALIDATE_EMAIL) === FALSE) {
$errors[]= ["name"=>"email","error"=>"invalid Email"];
}
if(filter_var($password, FILTER_VALIDATE_REGEXP,["options"=> [ "regexp" => "/.{6,25}/"]]) === FALSE){
$errors[]= ["name"=>"password","error"=>"invalid password (6 to 25 characters)"];
}
if($password !== $repeatPassword){
$errors[]= ["name"=>"repeatPassword","error"=>"passwords don't match"];
}
if (count($errors) === 0) {
$salt= uniqid(mt_rand(), true);
$options=['salt'=>$salt, 'cost'=>12];
// everything is OK, the browser should send us to the next page
$sql = "INSERT INTO username (username,password, eMail ,joinedDate, active, activecode) VALUES (:username, :password, :email ,NOW(), 0, :code)";
$query = $db->prepare($sql);
$query->execute();
$json[] = array("username" => $username, "password" => $password, "email" => $email, "code" => $code);
// $json = json_encode($json);
// $json = file_get_contents('php://input');
$obj = json_decode($json,true);
echo json_encode(["message"=>"Please view your email account to activate your account"]);
}else{
echo json_encode(["errors"=>$errors]);
}
?>
First attempt before deletion.
This didn't work I'm not sure why, when I attempted to use echo json_encode(["message"=>"Please view your email account to activate your account"]);
within if (count($errors) === 0) {}
it wasn't working.
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
$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(["message"=>"Please view your email account to activate your account"]);
}
echo json_encode($errors);
This section here is my problem, without it my success message will show up with it my success message will not show up.
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");