我试图检查输入的用户名和电子邮件与数据库检查其可用性。AJAX请求不显示信息
它很好地检查电子邮件。我使用相同的代码的电子邮件,但不同的ID标签:
这里是我的用户名形式:
Desired Username
这里是功能检查:
function checkname()
{
var username=document.getElementById("desusername").value;
if(username) {
$.ajax({
type: 'post',
url: '../members/authentication/checkusername.php',
data: {
username:username,
},
success: function (response) {
$('#name_status').html(response);
if(response=="OK") {
return true;
}else {
return false;
}
}
});
}
else {
$('#name_status').html("");
return false;
}
}
这里是我的php脚本来检查:
require_once("../../includes/database.class.php");
if(isset($_POST['desusername'])){
$username = $_POST['desusername'];
$sql = "SELECT username FROM users WHERE username = '$username'";
$result = $database->query($sql);
if(mysqli_num_rows($result) > 0) {
?>
Username already exists. Please choose a different one.
}else{
?>
Valid. You are good to go!
}
exit();
}
?>
现在,在开发人员控制台它显示其调用函数everyt IME I型:
XHR finished loading: POST "http://localhost/members/authentication/checkusername.php".
但没有在相关的div显示(#name_status)..
+0
做的console.log(Response),并检查该值 –
+0
@KarthikGanesan确定,它想出这样的:'未捕获的ReferenceError:检查名未在HTMLInputElement.onkeyup' –
+0
定义 也许这添加到阿贾克斯调用'contentType:“application/json”' –