我只是无法弄清楚这段代码有什么问题。
我只是向服务器发送用户名和密码,然后服务器发回响应。服务器写入数据库没有问题,但在客户端有时它不会到达if(xmlhttp.readyState==4 && xmlhttp.status==200)
. 并在它执行 line 之后alert('login5')
,jquery 动画重置。我知道这是 php 问题,但我不知道为什么它有时有效但有时无效,感谢您的帮助。
<script type = "text/javascript">
function sendLogin(){
var xmlhttp;
var getString;
var url = "login.php";
var username=document.getElementById('name').value;
var password=document.getElementById('pw').value;
var url= url+ "?username="+username+"&password="+password;
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("get", url , true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
alert("reached inside");
getString = xmlhttp.responseText;
alert(getString);
}
}
xmlhttp.send();
alert('login5');
//problem here, have to wait a while
}
</script>
html代码:
<form id="logInBoxes">
<input type="text" placeholder="username" id='name' size="15px">
<input type="password" placeholder="pw" id='pw' size="10px">
<input type="submit" value="Log In" onclick='sendLogin()'>
</form>
php代码:
<?php
$username= $_GET['username'];
$password= $_GET['password'];
$salt = mcrypt_create_iv(32, MCRYPT_RAND);
$password = crypt($password, $salt);
$salt = mysql_real_escape_string($salt);
$password = mysql_real_escape_string($password);
$sql = mysqli_connect('localhost','root','','housescale');
// Check connection
if (mysqli_connect_errno()){echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($sql, "INSERT INTO user (username, password, salt)
VALUE
('$username', '$password','$salt')") or trigger_error(mysql_error());
mysqli_close($sql);
echo $username;
?>
编辑:如果我alert('login5')
在一个循环中再做 9 次,它就会起作用。这个延迟修复究竟意味着什么?