gpt4 book ai didi

javascript - 没有 JQuery 的 AJAX POST 到 PHP

转载 作者:行者123 更新时间:2023-11-29 18:00:21 25 4
gpt4 key购买 nike

我有一个 PHP 作业,我决定尝试将 AJAX 添加到其中,因为在我们的类里面,我们不会只学习 PHP 来学习 AJAX。我似乎无法得到回应。但是,在 Fire Fox 控制台的网络部分中,我可以找到 POST 发送,其中包含我在表单中输入的值,以及 PHP 函数运行后的 PHP 回显。但是,它不会出现在我的 div 标签中。非常欢迎任何帮助,谢谢。

这是 HTML 和 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!";
} ?>

最佳答案

尽量使用函数,使代码可重用,更容易排错。我建议使用以下代码。

<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">

function createAjaxRequestObject() {
var xmlhttp;

if(window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

// Create the object
return xmlhttp;
}

function AjaxPost(ajaxURL, parameters, onComplete) {
var http3 = createAjaxRequestObject();

http3.onreadystatechange = function() {
if(http3.readyState == 4) {
if(http3.status == 200) {
if(onComplete) {
onComplete(http3.responseText);
}
}
}
};

// Create parameter string
var parameterString = "";
var isFirst = true;
for(var index in parameters) {
if(!isFirst) {
parameterString += "&";
}
parameterString += encodeURIComponent(index) + "=" + encodeURIComponent(parameters[index]);
isFirst = false;
}

// Make request
http3.open("POST", ajaxURL, true);
http3.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http3.send(parameterString);
}

function completedAJAX(response) {
alert(response);
}

function testPass() {
var parameters = {
"uname" : document.getElementById("uname").value,
"passwd" : document.getElementById("passwd").value
};

AjaxPost("authenticate.php", parameters, completedAJAX);
}
</script>
</head>
<body>
<p>
<form id="login">
<strong>Login</strong><br/><br/>
Username :<br/><input type="text" name="uname" id="uname" /><br/>
Password :<br/><input type="password" name="passwd" 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>

另一个提示是,如果您在测试某些脚本时使用 Firefox 或 Chrome 中的开发人员工具(按 CTRL + SHIFT + J),请使用 Firebug。这个错误会在控制台中弹出,您会立即注意到这个错误。

关于javascript - 没有 JQuery 的 AJAX POST 到 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35418677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com