php
  • ajax
  • post
  • login
  • 2014-01-11 52 views -1 likes 
    -1

    何所有,在這裏我解釋我的問題(據我紅色我沒有找到任何工作解決方案)。 這裏我聯繫我的文件:ajax post方法和php

    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問題,我真的很難找出解決方案。

    回答

    2

    這是您的數據:

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

    這是要檢查什麼:

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

    你應該改變utenteuser或周圍的其他方法。在你的表單中,你也使用user,所以我會建議在任何地方使用。

    所以:

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

    它的工作了,太感謝你了。 –

    +0

    @ user3184908不客氣,請不要忘記將anwer標記爲已接受,如果它解決了您的問題。 – jeroen

    相關問題