I'm trying to send an authentication response in json through the ajax.
In chrome devtools, I get the response of the file register.ajax.php
on the "NETWORK" but never it is never accepted in javascript file.
In the user.class.php, I check if either the userid/username or the email already exist, if they do, it will send the status as "error" or else "success";
user.class.php
public function register_client($uid, $email, $pwd)
{
$this->db = DB::getInstance();
$stmt = $this->db->prepare("SELECT * FROM user_accounts WHERE uid = :uid OR email = :email");
$stmt->bindParam(":uid", $uid);
$stmt->bindParam(":email", $email);
$stmt->execute();
$query = $stmt->fetch(PDO::FETCH_ASSOC);
$resultado_uid = $query['uid'];
$resultado_email = $query['email'];
if ($resultado_uid || $resultado_email)
{
$response_array['status'] = 'error';
echo json_encode($response_array);
}
else
{
$response_array['status'] = 'success';
echo json_encode($response_array);
$basic_password = $pwd;
$hashed_password = password_hash($basic_password, PASSWORD_BCRYPT);
$stmt = $this->db->prepare("INSERT INTO user_accounts(uid, email, pwd) VALUES (:uid, :email, :pwd)");
$stmt->bindParam(":uid", $uid);
$stmt->bindParam(":email", $email);
$stmt->bindParam(":pwd", $hashed_password);
$stmt->execute();
}
}
<?php
require_once "../header.php";
if(isset($_POST['action'])) $action = $_POST['action'];
if($action == 'register_client'){
$cliente->register_client($_POST['uid'], $_POST['email'], $_POST['pwd']);
}
<?php
require_once "header.php";
require_once "nav.php"
?>
<form action="" method="POST">
<input type="text" name="uid" id="uid" placeholder="username"><br>
<input type="email" name ="email" id="email" placeholder="email"><br>
<input type="password" name="pwd" id="pwd" placeholder="password"><br>
<button name="register_client" id="register_client" type="submit">Register</button>
</form>
<?php
include "footer.php";
?>
$(document).ready(function() {
$("#register_client").on("click", function() {
var uid = $("#uid").val();
var email = $("#email").val();
var pwd = $("#pwd").val();
$.ajax({
type: 'post',
url: 'ajax/register.ajax.php',
dataType: 'json',
data: {
'uid': uid,
'email': email,
'pwd': pwd,
'action': 'register_client',
},
success: function(data) {
console.log(data);
},
error: function() {
console.log("it failed");
}
});
return false;
});
});
in register.ajax.php
you are not echo any think.if you want to get output then you have to echo json or string in register.ajax.php
in register.ajax.php
if you add echo "success";
then you will get it in sucess console log
if you want to return array then you can use echo json_encode($yourArrayData);