【发布时间】:2015-01-05 13:35:43
【问题描述】:
我想使用 AJAX 将登录电子邮件和密码发布到 PHP 页面
<form onsubmit="authenticate()">
<input type="text" placeholder="E-mail" name="email" id="email" />
<input type="password" placeholder="Password" name="password" id="password" />
<input type="submit" value="Login"/>
</form>
AJAX:
function authenticate() {
var email = document.getElementById("email").value;
var pass = document.getElementById("password").value;
var params = 'email=' + email + '&pass=' + pass;
var httpc = new XMLHttpRequest(); // simplified for clarity
var url = "http://127.0.0.1/login/login.php";
httpc.open("POST", url, true); // sending as POST
httpc.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
httpc.setRequestHeader("Content-Length", params.length); // POST request MUST have a Content-Length header (as per HTTP/1.1)
httpc.onreadystatechange = function() { //Call a function when the state changes.
if(httpc.readyState == 4 && httpc.status == 200) { // complete and no errors
if(httpc.responseText == "success") {
window.location.replace("files.html");
}
else if (httpc.responseText == "fail")
alert("Invalid details");
}
httpc.send(params);
}
}
如果选择查询在认证后返回一行,则回显“成功”,否则回显“失败”
【问题讨论】:
-
1).你的问题不清楚。2).在你的 Javascript 中你是如何传递参数的?
-
我有一个页面
login.html必须使用 AJAX 将数据发布到login.php。我在一些教程中得到了这个,但这不是我尝试的唯一方法。我一直没有得到结果。 -
试试这个教程...没有 jquery 1).w3schools.com/php/php_ajax_database.asp 。使用 Jquery 2).formget.com/submit-form-using-ajax-php-and-jquery
-
不工作。我不知道是什么问题。我过去创建过 PHP 应用程序,但都在 htdocs 下,但这个 html 文件不是,我不能将其保留为 PHP 页面。我直接用 PHP 尝试了 GET,所以我的 PHP 绝对没问题。