I am trying to get my jQuery synchronous ajax call to return a boolean value, but all I get when I test the return value is 'undefined'.
jQuery code:
function foo() {
var username = "hello"
if (checkUsernameExist(username)) {
//do something here
}
}
function checkUsernameExist(username) {
return $.ajax({
type: 'POST',
url: "check_username.php",
data: { username: username},
async: false,
}).responeText;
}
PHP code:
<?php
//sets up connection (among other things)
include 'common.php';
//check we have username post var
if(isset($_POST['username']))
{
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die();
}
$un = $_POST['username'];
//check username in db
$results = pg_query($con,"SELECT * FROM users WHERE username='{$un}'");
//return total count
$username_exist = pg_num_rows($results); //total records
if ($username_exist == 1) {
echo true;
} else {
echo false;
}
}
?>
Thanks in advance.