2013-05-03 105 views
0

我有一個帶登錄表單的PHP頁面。當單擊表單時,將執行後端驗證,並且json返回登錄驗證的狀態。當用戶登錄成功地在從ajaxComplete - 表單提交/驗證問題

// When DOM is ready 
$(document).ready(function(){  
    // When the form is submitted 
    $("#status > li > form").submit(function(){ 

     // Hide 'Submit' Button 
     $('#submit').hide(); 

     // Clear the error messages if any 
     $('#error_response').html(""); 

     // 'this' refers to the current submitted form 
     var str = $(this).serialize(); 

     var ran_no = new Date().getTime();     
     str += "&ran="+ran_no; 

     // -- Start AJAX Call -- 
     $.ajax({ 
       type: "POST", 
       url: "process.php", // Send the login info to this page 
       data: str, 
       dataType:'json', 
       success: function(msg){ 
        $(document).ajaxComplete(function(event, request, settings){ 

        // Hide Gif Spinning Rotator 
        $('#ajax_loading').hide(); 

        // Show 'Submit' Button 
        $('#submit').show(); 

        if(msg.error_array == "OK") // LOGIN OK? 
        { 
        var login_response = '<li><div class="alert alert-success" id="ajax_response"><i class="icon-spinner icon-spin pull-left"></i> Succes...</div></li>'; 
        // Replace the dropdownlist contents 
        $('#status').html(login_response); // Refers to 'status' 
        // After 3 seconds redirect the 
        setTimeout('go_to_private_page()', 3000); 
        } 
        else // ERROR? 
        { 
         var login_response = ""; 
         $.each(msg.error_array,function(i,element) 
         { 
         login_response = "<div class=\"alert alert-error\">" + element + "<br>"; 
         }); 
         $('#ajax_response').html(login_response); 
        } 
        }); 
        } 
       }); 
       // -- End AJAX Call -- 
     return false; 
    }); // end submit event 

    }); 

正被調用的登錄驗證的PHP頁面返回JSON對象

function procLogin(){ 
    global $session, $form;  
    /* Login attempt */ 

    // database call 
    $retval = $session->login($_POST['username'], $_POST['password'], isset($_POST['remember_me'])); 
    $results = array(); 
    /* Login successful */ 
    if($retval){ 
     $results['error_array'] = array('OK'); 
     echo json_encode($results); 
    } 
    /* Login failed */ 
    else{ 
    $_SESSION['value_array'] = $_POST; 
    $_SESSION['error_array'] = $form->getErrorArray(); 
    $results['value_array'] = $_POST;   //Holds submitted form field values 
    $results['error_array'] = $form->getErrorArray(); //Holds submitted form error messages 
    echo json_encode($results); 
    } 
} 

代碼點擊形式的提交按鈕時執行第一次,一切正常。

如果用戶第一次輸入無效密碼並進行更正,他仍會在第二次嘗試時收到無效密碼消息。

不使用ajax/json的相同代碼工作正常。

這裏有什麼問題?

回答

0

它可能是緩存問題。嘗試添加:

cache: false 

給您的Ajax調用。

+0

謝謝,我添加了參數,但沒有運氣。當使用Firebug來監控process.php的調用時,我確實注意到這次調用是第一次使用username = foouser&password = test123&sublogin = 1&action = user_login&ran = 1367612282272,而第二次當我測試username = correctuser&password = test123&sublogin = 1&action = user_login&ran = 1367612286789。所以調用使用更新的表單值。 – user2215655 2013-05-03 20:18:49