1 回答

TA貢獻1789條經(jīng)驗 獲得超10個贊
首先,您可以檢查請求是否作為 POST 請求發(fā)送(在瀏覽器中打開 register.php 將是 GET 請求)。
您可以通過這樣的方式包裝您的表單處理
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// check if username exists
if (isset($_POST['username'])) {
echo 'username: ' . $_POST['username'];
die;
} else {
echo 'no username';
die;
}
}
相應(yīng)地更改代碼,用于echo json_encode($data)以 JSON 格式返回數(shù)據(jù)。
在您的請求中,您可能需要添加正確的標頭來告訴 PHP 如何解釋隨請求一起發(fā)送的正文。
function validateUsername(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Typical action to be performed when the document is ready:
console.log(xhttp.responseText);
}
};
// add this line
xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhttp.open("POST", "../obrasci/registracija.php", true);
xhttp.send("username="+username.value);
}
另外,請確保您的命名正確。在你的代碼示例中,你將你的輸入稱為變量username,但是你將事件偵聽器添加到kor_ime,我不知道你是否將某些內(nèi)容更新為英語并忘記了它的其他部分以解決這個問題或者這是否是你的實際代碼
let username= document.getElementById('username');
username.addEventListener('input', validateUsername); // here change kor_ime to username and update the function name, you can omit the extra function wrapper
- 1 回答
- 0 關(guān)注
- 135 瀏覽
添加回答
舉報