繁体   English   中英

Ajax发布方法和PHP

[英]ajax post method and php

all,在这里,我解释我的问题(据我所知,我没有找到任何可行的解决方案)。 在这里,我链接我的文件:

progetto.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);

}

ajaxLogin.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;
}

?>

问题是,即使我使用正确的用户名和密码,我也始终会收到错误登录消息。 这是一个POST问题,我真的很难找出解决方案。

这是您的数据:

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM