wydtron wydtron - 3 years ago 113
PHP Question

Ajax not triggering success function

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";


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);
$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);

$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);


Through the ajax file I simply activate the register_client public function whenever the action 'register_client' is set (seems also fine);


require_once "../header.php";
if(isset($_POST['action'])) $action = $_POST['action'];

if($action == 'register_client'){

$cliente->register_client($_POST['uid'], $_POST['email'], $_POST['pwd']);


Register form should be fine(It is just a simple form, the action matches too)


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>



include "footer.php";


Here is where the problem seems to be emanating, The success function is not retrieving any data. Tried to do console.log(data) in it to see what it gives me, and so far nothing.

$(document).ready(function() {

$("#register_client").on("click", function() {
var uid = $("#uid").val();
var email = $("#email").val();
var pwd = $("#pwd").val();

type: 'post',
url: 'ajax/register.ajax.php',
dataType: 'json',
data: {
'uid': uid,
'email': email,
'pwd': pwd,
'action': 'register_client',

success: function(data) {
error: function() {
console.log("it failed");

return false;


Answer Source

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);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download