所以我有這個(預處理語句)的mysqli查詢在PHP(部分代碼):如何使用AJAX來執行mysqli的查詢(預處理語句)
#New DB connection
@ $mysqli = new mysqli ('localhost', 'user', 'password', 'database');
#Check DB connection, print error upon failure
if (mysqli_connect_errno()) {
printf("<p>Connect failed.</p> <p>Error: \"%s\"</p>\n", mysqli_connect_error());
exit();
} #if
#Check if userName already exists
$query = "SELECT * FROM users WHERE userName = ?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("s", $registerUserName);
$stmt->execute();
$stmt->store_result();
$num_results = $stmt->num_rows;
if ($num_results) {
echo "<p>This user already exists. Please click your back button.</p>";
exit;
}; #if
而且我想通過JavaScript執行此/ AJAX上onblur事件:
function checkForUser(str) {
var xmlhttp;
if (str=="") {
return;
}
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open(???,true); // not sure what goes here or if I should even use this method
xmlhttp.send();
}
所以這是可能的嗎?如何?我找到了一些教程,但他們涉及使用jQuery。我可以做到沒有jQuery嗎?
問題文件的名稱是什麼(請求應該在哪裏)? '$ registerUserName'也從哪裏獲得值? – kero
只要做一個簡單的谷歌搜索「阿賈克斯教程」,取代回調網址到您的PHP腳本張貼您需要的用戶名,你應該很好去。 – Kypros