I currently have the following function in my functions.php
which checks if a username exists in the DB.
function check_username() {
$username = $_POST['user'];
if ( username_exists( $username ) ) {
$return['user_exists'] = true;
}
else {
$return['user_exists'] = false;
}
echo json_encode($return);
die();
}
add_action('wp_ajax_check_username', 'check_username');
I'm using Ajax to call that function with an appropriate username:
$.ajax({
url : "http://examle.com/wp-admin/admin-ajax.php",
type : "GET",
dataType : "json",
cache : false,
data : {
action : 'check_username',
user: 'test'
},
success : function (json) {
if (json.user_exists) {
alert(json.user_exists);
}
}
});
The problem is that $username
returns null
instead of test
. I've been trying for several hours now different combinations yet they've all returned null.
Assigning this makes the above php function work $username = 'test';
so I'm pretty sure this isn't an issue with the php code.
So my question is, how do I get the variable $username
to listen and fetch the correct parameter instead of null
?