2013-10-13 32 views
1

我有這個按鈕,我想要它做的是驗證用戶並隱藏#dialogcontainer完成對用戶的認證後隱藏div對話框

<button onclick=" authenticate();">Register</button> 

每當我檢查xmlhttp.responseText,它工作正常,但是當我嘗試把它分配給了條件比較一個變量,那就是當我得到的問題。

我想要做的是檢查天氣用戶存在使用login.php文件,並根據xmlhttp.responseText我想關閉對話框。

<script> 
function login(){ 
       var username= $("#txtloginusername").val(); 
       var password = $("#txtloginpassword").val(); 


       if (window.XMLHttpRequest) 
        { 
         xmlhttp=new XMLHttpRequest(); 
        } 
       else 

         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
        } 

       xmlhttp.onreadystatechange=function() 
        { 
         if (xmlhttp.readyState==4 && xmlhttp.status==200) 
         { 
          //var status = document.getElementById ("logindisplay").innerHTML=xmlhttp.responseText; 
          var status =xmlhttp.responseText; 

          $("#logindisplay").html(status); 
          /*if(status=="yes") 
           { 
            $("#logindisplay").html("user exist"); 
           } 
           else 
            { 
             //$("#dialogcontainer").slideUp(); 
             $("#logindisplay").html("NO"); 
            }*/ 
         } 
        } 
        xmlhttp.open("GET","login.php?username="+username +"&password=" + password,true); 
        xmlhttp.send(); 


      } 
</script> 

這是用戶身份驗證對話框的div,我想它來完成認證

<div id="dialogcontainer"> 
      <div id="dialog"> 
       <h3>Welcome to Talk Login</h3> 
       <p>Username</p> 
       <input type="text" id="txtloginusername"/> 
       <p>Password</p> 
       <input type="password" id="txtloginpassword"/> <br /><br /> 
       <button onclick="login()">Login</button> 
       <button onclick="javascript:$('#dialogcontainer').slideUp();">Exit</button> 
       <br> 
       <br> 
       <div id="logindisplay" class ="display"></div> 

      </div> 
</div> 

的login.php文件

<?php 


$username= $_GET["username"]; 
$password= $_GET["password"]; 

$db = mysql_connect("localhost","root","") or die (mysql_error()); 
mysql_select_db("esk",$db); 

$sqlcommand = "SELECT * FROM user WHERE username = '$username'"; 
$result = mysql_query($sqlcommand,$db); 


if(mysql_num_rows($result)>=1) 
    { 
    echo "yes"; 
    } 
else 
    { 
    echo "no"; 

    } 
//$result = mysql_query($sqlcommand,$db);*/ 
?> 
+0

小事:你好像在你的代碼上有太多''''。最後一個。這是一個錯字嗎? – Sergio

回答

0

你可以做這樣的事情後消失:

function login() { 
    var username= $('#txtloginusername').val(); 
    var password = $('#txtloginpassword').val(); 

    $.ajax({ 
     url: 'login.php', 
     type: 'POST', 
     data: { 
     username: username, 
     password: password 
     }, 

     success: function(response) { 
     if (response === 'yes') { 
      $('#logindisplay').html('user exist'); 
     } else { 
      $('#logindisplay').html('NO'); 
     } 
     }, 

     error: function(xhr) { 
     // do something nice with errors you might get 
     } 
    }); 
} 

只要此對話框用於登錄,則不應使用GET請求,而應使用POST