2012-11-17 64 views
2

我的代碼運行良好,但在成功調用時失敗。在ajax節「if(result.success){」它顯示瀏覽器源選項卡中的錯誤,結果爲空。result.success在Ajax調用中失敗

登錄頁面

<form id="login_form_header"> 
    <input type="hidden" name="action" value="sc_ajax_callback" /> 
    <input type="hidden" name="func" value="sc_login" /> 

    Email: 
    <input type="text" name="username" /> 

    Password: 
    <input type="password" name="password"/> 

    <input type="submit" value="Log in" /> 
</form> 
<script> 
$("#login_form_header").submit(function(event){ 

    event.preventDefault(); 

    $.ajax({ 
     type: 'post', 
     url: '<?php echo get_stylesheet_directory_uri(); ?>/connect.php', 
     data: $('#login_form_header').serialize(), 
     dataType: 'json', 
     success: function(result){ 
      if (result.success){ 
       window.location = "my-dashboard/"; 
       return false; 
      }; 
     }, 
     error: function(e){console.log("Could not retrieve login information")} 
    }); 

    return false; 
}); 
</script> 

連接頁面

<?php 

    mysql_connect("localhost", "%user%", "%pass%") or die(mysql_error()); // Connect to database server(localhost) with username and password. 
    mysql_select_db("%db%") or die(mysql_error()); // Select registration database. 
    # Make sure form data was passed to the script 
    if(isset($_POST['username']) && !empty($_POST['username']) AND isset($_POST['password']) && !empty($_POST['password'])){ 

    # Define Variables 
    $active_var = "1"; 
    $given_username = mysql_escape_string($_POST['username']); 
    $given_password = dosomethingtopass; 
    $matched_username = ""; 
    $matched_password = ""; 

    # See if there is matching info in the database 
    $sql = 'SELECT username, password, active FROM %table% WHERE username="'.$given_username.'" AND password="'.$given_password.'" AND active="'.$active_var.'"'; 
    $result = mysql_query($sql); 
    while($row = mysql_fetch_assoc($result)){ 
    if($given_password == $row['password']){ 
     $matched_username = $row['username']; 
     $matched_password = $row['password']; 
     } 
    }; 

    # If there was a match 
    if($matched_username != "" && $matched_password != ""){ 
     #If there is only one result returned 
     echo json_encode(array("$matched_password")); 
     $session_sql = 'SELECT id, username, password, last_login FROM %table% WHERE username="'.$matched_username.'" AND password="'.$matched_password.'"'; 
     $session_result = mysql_query($session_sql); 
     while($returned_row = mysql_fetch_assoc($session_result)){ 
      if($matched_password == $returned_row['password']){ 


      $_SESSION['id'] = $returned_row['id']; 
      //$_SESSION['last_login'] = $returned_row['last_login']; 
      $_SESSION['username'] = $returned_row['username']; 
       } 
      } 

      $date = date('Y-m-d H:i:s'); 
      $update_sql = "UPDATE %table% SET last_login='".$date."'"; 
      mysql_query($update_sql); 

     echo json_encode(array("success"=>"user logged in", "session"=>$_SESSION)); 
     } 
    } 

?> 

另外,update_sql正在更新在我的表中的每一行有點兒奇怪。不知道我去哪裏錯了任何幫助?

編輯:

更新

$.ajax({ 
     type: 'post', 
     url: '<?php echo get_stylesheet_directory_uri(); ?>/connect.php', 
     data: $('#login_form_header').serialize(), 
     dataType: 'json', 
     success: function(result){ 

       window.location = "my-dashboard/"; 

      }; 
     }, 
     error: function(e){console.log("Could not retrieve login information")} 
    }); 

回答

0
success: function(result){ 
      if (result.success){ 
       window.location = "my-dashboard/"; 
       return false; 
      }; 

變化

success: function(result){      
        window.location = "my-dashboard/";      
       } 

result這裏是服務器,而不是一個對象發送TE數據。


Also the update_sql is updating every row in my table kinda odd

這是因爲在你的UPDATE查詢沒有WHERE條款。

從文檔:

The WHERE clause, if given, specifies the conditions that identify which rows to update. With no WHERE clause, all rows are updated.

更多信息here

編輯更新

後,你有一個額外};

變化

success: function(result){ 

       window.location = "my-dashboard/"; 

      }; 
     }, 

success: function(result){ 

       window.location = "my-dashboard/"; 


     }, 

幾個點值得一提:

  • 棄用通知:jqXHR.success(),jqXHR.error()和jqXHR.complete()回調將在jQuery 1.8中被棄用。要準備代碼以便最終刪除它們,請改用jqXHR.done(),jqXHR.fail()和jqXHR.always()。

  • mysql_ *函數被棄用。改用PDO。

0

就試試這個:

success: function(result){ window.location = "my-dashboard/"; }

因爲這個函數調用成功的結果。 第一個參數是來自服務器答案的數據。這不是對象。 你能在那裏找到細節:http://api.jquery.com/jQuery.ajax/

+0

更新了這個問題,它有點作用,但現在我在控制檯中得到「Uncaught SyntaxError:Unexpected token;」。 – shayward

+0

從您的更新代碼中刪除此字符串:'};' – newman