我想发送一个帖子请求并从php文件中获取一个回音。但我无法找到我的错误,总是得到一个通知:未定义索引...
请不要jquery ......
<form id="register" method="POST" action="register-action.php">
<input type="text" name="first_name" placeholder="Onoma"><br>
<input type="text" name="last_name" placeholder="Epitheto"><br>
<input type="text" name="mail" placeholder="E-mail"><br>
<input type="password" name="pass" placeholder="password"><br>
<select name="eidikotita_id">
<?php while($eid = $eidikotites->fetch( PDO::FETCH_ASSOC )):
echo "<option value={$eid['id_eidikotitas']}>{$eid['titlos']}</option>";
endwhile; ?>
</select>
<input type="submit" value="Register"><br>
</form>
/////////////////////////////////////////////// ////////////////
var mail_input = document.querySelector("#registerinput[name='mail']").value;
alert(mail_input);
document.querySelector("#register input[name='first_name']").focus();
document.querySelector("#register input[name='mail']").onblur = function() {
var request = new XMLHttpRequest();//instantiate an XMLHttpRequest object
request.open("POST", "register-action.php", true);//Specifying the Request
request.setRequestHeader("Content-Type", "text/plain");//POST requests,for example, need a “Content-Type” header
request.send(mail_input);//specify the optional request body. GET requests never have a body, so you should pass null or omit the argument.
};
/////////////////////////////////////////////// //////////////////////////////
<?php
echo $_POST['mail'];
?>
答案 0 :(得分:0)
只需print_r()
POST数据即可查看问题所在。
xhr.send()
应如下所示:xhr.send('mail=' + encodeURIComponent(email))
因为您需要$_POST['mail']
。您的选择器也存在问题"#register input[name='mail']"
。
答案 1 :(得分:0)
document.querySelector("#register input[name='first_name']").focus();
document.querySelector("#register input[name='mail']").onblur = function() {
var mail_input = document.querySelector("#register input[name='mail']").value;
var request = new XMLHttpRequest();//instantiate an XMLHttpRequest object
request.open("GET", "register-action.php?mail="+mail_input, true);//Specifying the Request
request.setRequestHeader("Content-Type", "text/plain");//POST requests,for example, need a “Content-Type” header
request.send(encodeURIComponent(mail_input));//specify the optional request body. GET requests never have a body, so you should pass null or omit the argument.
//////send succcess
if (request.readyState === 4 && request.status === 200) {
request.onreadystatechange = function(){
console.log(request.responseText);
}
}
};