ajax post method and php



大家,在这里我解释我的问题(就我的红色而言,我没有找到任何可行的解决方案)。在这里我链接我的文件:

普罗格托.html

<html> 
<head> 
    <script type="text/javascript" src="funzioni.js"></script>
    <title>Pagina iniziale</title>
</head>
<body align='center'>
    <p>Gymnasium</p>
    <p>icona</p>
    <form id="ajaxForm" name="ajaxForm">
        <table align="center">
            <tr>
                <td><label>Utente</label></td>
                <td><input type="text" name="user" id="user" value="" /></td>
            </tr>
            <tr>
                <td><label>Password</label></td>
                <td><input type="password" name="password" id="password" value="" /></td>
            </tr>
        </table>
        <input type="button" id="submit" name="submit" value="Accedi" onclick='submitForm()' />
    </form>
    <div id="risultato"></div>
</body>

JavaScript 文件

function createXMLHttpRequestObject(){
if (window.XMLHttpRequest) { return new XMLHttpRequest(); }
if (window.ActiveXObject) { return new ActiveXObject(Microsoft.XMLHTTP); }
return null;

}

function submitForm(){
var ajax = createXMLHttpRequestObject();
ajax.onreadystatechange = function () {
                        if (ajax.readyState==4 && ajax.status==200){
                        var response = ajax.responseText;
                        document.getElementById("risultato").innerHTML = response;
                    }
                }
ajax.open("post", "ajaxLogin.php", true);
var data = "utente=" + document.getElementById('user').value + "&password=" + document.getElementById('password').value; 
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajax.send(data);

}

ajax登录.php

<?php
if (!isset($_POST["user"]) || !isset($_POST["password"])){
    die("Bad login");
}
$user = $_POST['user'];
$pwd = $_POST['password'];
if ( (($user == "angel") && ($pwd == "devil")) || (($user == "john") && ($pwd == "smith")) ){
    $response = "Benvenuto  " . $user;
    echo $response;
}

?>

问题是即使我使用了正确的用户名和密码,我也总是收到错误的登录消息。这是一个开机自检问题,我真的很难找到解决方案。

这是您的数据:

var data = "utente=" + document.getElementById('user').value + "&password=" + document.getElementById('password').value;

这就是您正在检查的内容:

if (!isset($_POST["user"]) || !isset($_POST["password"])){

您应该utente更改为user或相反。在您的表单中,您也在使用user,所以我建议在任何地方使用它。

所以:

var data = "user=" + document.getElementById('user').value + "&password=" + document.getElementById('password').value;

最新更新