没有JQuery的AJAX POST到PHP [英] AJAX POST to PHP without JQuery
问题描述
我有一个PHP作业,我决定尝试添加AJAX,因为在我们的课程中我们不会只学习AJAX。我似乎无法得到工作的回应。然而,在我的Fire Fox控制台的网络部分,我可以找到POST发送,其中包含我输入到表单中的值和PHP回显作为php函数工作的结果。但是,它不会出现在我的div标签中。任何帮助都是最受欢迎的,谢谢。
I had a PHP assignment that I decided that I wanted to try and add AJAX to because in our class we will not learn AJAX just PHP. I can't seem to get the response to work. However in the network section of my console on Fire Fox I can find the POST send with the values I entered into the form and the PHP echo as a result from the php function working. But, it will not show up in my div tag. Any help would be most welcome, thanks.
这是HTML和JavaScript:
Here is the HTML and JavaScript:
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
function testPass() {
var username = document.getElementById("uname").value;
var passwrd = document.getElementById("passwd").value;
var creds = "uname="+username+"&passwd="+passwrd;
var ajx = new XMLHttpRequest();
ajx.onreadystatechagne = function () {
if (ajx.readyState == 4 && ajx.status == 200) {
document.getElementById("message").innerHTML = ajx.responseText;
}
};
ajx.open("POST", "authenticate.php", true);
ajx.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajx.send(creds);
//document.getElementById("message").innerHTML = creds;
}
</script>
</head>
<body>
<p>
<form id="login">
<strong>Login</strong><br/><br/>
Username :<br/><input type="text" id="uname"><br/>
Password :<br/><input type="text" id="passwd"><br/><br/>
<div id="message" style="color:red;"></div>
<br/><button type="button" value="Submit" onclick="testPass();">Sign In</button>
</form>
</p>
</body>
</html>
这是PHP:
<?php
$uname = $_POST['uname'];
$passwd = $_POST['passwd'];
if (empty($uname) && empty($passwd)) {
echo "The username and password are required!";
} else if (empty($uname)) {
echo "The username is required!";
} else if (empty($passwd)) {
echo "The password is required!";
} else {
echo "It works!";
} ?>
推荐答案
你有一个错字: ajx.onreadystatechagne
应为 ajx.onreadystatechange
这篇关于没有JQuery的AJAX POST到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!