我只是不知道這段代碼有什麼問題。客戶端和服務器php sql bug
我只是發送一個用戶名和密碼到服務器,然後服務器發送迴應。服務器寫入數據庫沒有問題,但在客戶端有時它不會到達if(xmlhttp.readyState==4 && xmlhttp.status==200)
內部。並在它執行行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次以上。這個延遲修復究竟意味着什麼?
我不是特別確定,我會在我的測試中使用我的整個代碼。 – Jason