2013-07-11 46 views
0

我有一個登錄表單在php中。我已經使用ajaxform,以便在用戶名或密碼錯誤時不會重新加載頁面。我有另一個頁面checklogin.php,它檢查用戶名和密碼是否在數據庫中,並返回行數。我想比較login.php中的計數,並在count = 1時顯示錯誤消息,如果count = 2,則顯示錯誤消息並重定向到另一個頁面。我嘗試使用target來顯示errordiv中的計數:'errordiv'並檢查其innerHTML,但未能這樣做。從ajax表格返回值

我的login.php

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script> 
    <script type="text/javascript" src="scripts/jquery.form.js"></script> 
<!--Slider-in icons--> 
<script type="text/javascript"> 


    function checklogin() 
    { 
    var request=$("#login-form").ajaxForm(
        { 

        target:'#errordiv' 

        }).abort(); 
       request.submit(); 
     var divmsg=document.getElementById("errordiv"); 
     if (divmsg.childNodes[0].nodeValue == "1") 
      { 
     divmsg.childNodesp[0].nodeValue="Sorry"; 
     alert ("Invalid username or password");} //I didn't get the alert as well a the divmsg content didn't change 


        }; 
</script> 

</head> 
<body> 

<!--WRAPPER--> 
<div id="wrapper"> 

<!--LOGIN FORM--> 
<form name="login-form" id="login-form" class="login-form" action="checklogin.php" method="post"> 


    <!--CONTENT--> 
    <div class="content"> 
    <!--USERNAME--><input name="un" type="text" class="input username" placeholder="Username" onfocus="this.value=''" required="required" /><!--END USERNAME--> 
    <!--PASSWORD--><input name="pw" type="password" class="input password" placeholder="Password" onfocus="this.value=''" required="required" /><!--END PASSWORD--> 
    </div> 
    <!--END CONTENT--> 

    <!--FOOTER--> 
    <div class="footer"> 
    <!--LOGIN BUTTON--><input type="submit" name="submit" value="Login" class="button" onclick="checklogin()" /><!--END LOGIN BUTTON--> 

    </div> 
    <!--END FOOTER--> 

</form> 
<div id="errordiv" class="errordiv"></div> 
<!--END LOGIN FORM--> 

checklogin.php

<?php 

include ('dbconn.php'); 
     $user = trim($_POST['un']); 
     $pass = trim($_POST['pw']); 
     $user = mysql_real_escape_string($user); 
     $pass = mysql_real_escape_string($pass); 
     $result=mysql_query("select Id from login where Username='$user' and Password='$pass'") or die (mysql_error()); 
     $result= mysql_fetch_assoc($result); 
    $num= count ($result); 
    echo $num; 
?> 

我怎樣才能獲得的login.php $ num的值沒有它顯示一個DIV和比較$的價值num並相應地在errordiv中顯示errormsg,或者如果$ num == 2重定向到另一個頁面。

+0

您處理程序代碼應移至成功回調。 – Orangepill

+0

可能重複的[如何從AJAX調用返回響應?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – elclanrs

+0

正在使用ajax調用和ajax形式相同? – user1583647

回答

1

更改您的代碼,包括更改div和提供警報被列入回調的代碼。

 var request=$("#login-form").ajaxForm(
       { 
        target:'#errordiv', 
        success: function (html) { 
         if ($("#errordiv).html() == "1") { 
          $("#errordiv).html("Sorry"); 
          alert ("Invalid username or password"); 
         } 
        } 
        }).abort(); 
       request.submit(); 
+0

不工作它將頁面重定向到checklogin.php並顯示1 – user1583647

0

也許嘗試使異步檢查要求和處理用戶反饋在AJAX回調:

JS:

$(function(){ 
     $("#login-form").ajaxForm(function(response){ 
      if(response.count===1){ 
       $("#errordiv").html("msg you want to show"); 
      }else if(response.count===2){ 
       //redirect 
      } 
     }); 
    }); 

PHP:

<?php 
    header("Content-Type: application/json"); 

    //your database check logic 
    $user = trim($_POST['un']); 
    $pass = trim($_POST['pw']); 
    $user = mysql_real_escape_string($user); 
    $pass = mysql_real_escape_string($pass); 
    $result=mysql_query("select Id from login where Username='$user' and Password='$pass'") or die (mysql_error()); 
    $result= mysql_fetch_assoc($result); 
    $num= count ($result); 

    //and return response in JSON 
    echo json_encode(array("count"=>$num)); 

>

希望這對你有幫助。

[編輯]

刪除表單提交按鈕內嵌JS函數調用:

onclick="checklogin()" 

,並把checklogin功能邏輯記錄準備回調,初始化表單當DOM準備

+0

沒有工作。它將頁面重定向到checklogin.php並顯示{「count」:1} – user1583647

+0

已更新的答案。 – Chickenrice

+0

它仍然被重定向到checklogin.php – user1583647