我正在做登录表单连接到mysql数据库,我想检查用户名是否存在于数据库内,而不是重新加载整个页面。我正在使用Ajax从服务器发送和接收数据。现在我被这个错误卡住了:“在HTMLInputElement.onkeyup中没有定义checkUsn”。有人能帮我一下吗?我试着用谷歌搜索它,但似乎我的代码是正确的。以下是我的代码
function checkUsn(){
var usn = document.getElementById("usn").value;
if(usn){
$.ajax({
type: 'post',
url: 'checkdata.php',
data: {
emp_username: usn,
},
success: function(response){
$('#status').html(response);
if (response == "OK"){
return: true;
}else{
return false;
}
}
});
}else{
$('#status').html("INCORRECT USN AND PW");
return false;
}
}checkdata.php
<?php
include 'db_config.php';
$conn = new mysqli($db_servername, $db_username, $db_password, $db_name);
if(isset($_POST['emp_username'])){
$usn = $_POST['emp_username'];
$checkdata = "SELECT emp_username FROM emp_details where emp_username='$usn'";
$query = mysqli_query($conn, $checkdata);
if(mysqli_num_rows($query) > 0){
echo "OK";
}else{
echo "Your Username not exist";
}
exit();
}
?>这是我的表格
<form class="modal-content animate" action="/login_action.php" method="post" onsubmit="return checkall();">
<div class="container">
<span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">×</span>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3"></div>
<img class="avatar img-responsive col-lg-6 col-md-6 col-sm-6 col-xs-6" src="img/employee_avatar.png" alt="Avatar">
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3"></div>
</div>
<div class="container">
<label for="usn"><b>Username</b></label>
<input id="usn" type="text" placeholder="Enter Username" name="usn" onkeyup="checkUsn();" required>
<label for="pw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="pw" required>
<button type="submit">Login</button>
<label>
<input type="checkbox" checked="checked" name="remember"> Remember me
</label>
</div>
<div class="container" style="background-color:#f1f1f1">
<button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button>
<span class="psw">Forgot <a href="#">password?</a></span>
</div>
<button type="button" onclick="btnClickTest()"> test </button>
<span id="status"> </span>
</form>提前谢谢你!
发布于 2018-08-11 11:09:45
嗨,伙计们,我刚刚在代码中遇到了打字错误,我写了return: true;而不是return true;现在我的代码可以顺利地工作了。
谢谢
发布于 2018-08-11 11:56:06
Try to change you success function like this :
success: function(response){
$('#status').html(response);
if (response == "succeed"){
return: true;
}else{
return false;
}
}https://stackoverflow.com/questions/51796057
复制相似问题