2012-01-30 35 views
3

我使用Codeigniter MVC框架和im掙扎與jquery Ajax。 我的問題是你如何在ajax成功函數中添加條件語句?ajax成功函數中的條件語句

基本上就是我想要做的就是在成功進入會重定向到另一個頁面,如果不將顯示一個錯誤:手機號碼不相符,或者如果空請填寫空白。

控制器方法

if($mobile_number == "") { 
        $data = array('message' => "Please fill up the blanks", 'success' => 'no'); 

      } 
      elseif($mobile_number != "123") { 
        $data = array('message' => "Please does not match", 'success' => 'no'); 

      }else { 
        #redirect('home/test'); 
        $data = array('message' => "Mobile match.", 'success' => 'yes'); 
      } 
      $output = json_encode($data); 
      echo $output; 

my_ajax.js

$(document).ready(function(){ 


$("#request_submit").click( 

function(){ 

    var mobtel=$("#mobtel").val(); 

    $.ajax({ 
    type: "POST", 
    url: "post_action", 
    dataType: "json", 
    data: "mob_tel="+mobtel, 
    cache:false, 
    success: function (data) { 

      if(data.success == 'yes') { 
        $("#form_message").html(data.message).fadeIn('slow'); 
      } 
      else if(data.success == 'no') { 
        alert('error'); 
      } 
    } 

    }); 
}); 
}); 

預先感謝。希望有人能幫助我。 (T_T)

+0

我不認爲你應該修剪'data',因爲它已經是一個解析對象而不是一個字符串。 – MMM 2012-01-30 11:44:18

+0

而且你的第二個'在你成功的功能if'的說法應該是和其他'聲明if'而是因爲'data.success'不能同時'「yes''和'」 no'' – MMM 2012-01-30 11:51:06

+0

更新我的代碼。仍然不起作用。您可以查看我的網站:http://apps.stratpoint.com:9114/home/request_verification/ – 2012-01-30 11:56:56

回答

3

不知道如果我明白你的問題很好......從什麼我得到這個應該推動到正確的方向。

如果我是你,我會改變我的下列形式迴應:

$data = array(
'message'=> /* Your message */, 
'success' => /* True or false */, 
'case'=> /* 1,2,3 something unique to tell the cases apart */,); 

echo json_encode($data); 

所以,jQuery中我可以簡單地這樣做:

$.ajax({ 
    type: "POST", 
    url: "post_action", 
    dataType: "json", 
    data: "mob_tel="+mobtel, 
    cache:false, 
    success: function (data) { 

     switch(data.case){ 
     1: /*First case */ 
     break; 
     2: /*Second... */ 
     break; 
     3: /* You know the drill... */ 
     break; 
     default: 
     /* If none of the above */ 

     } 
} 

}); 

由於有三種情況你的代碼,最好的做法是將它們作爲三種情況處理。更堅實的代碼,更不容易出錯。

我希望這有助於!

0

在控制器的方法:

在功能使用結束exit();

在my_ajax.js

$(document).ready(function(){ 


$("#request_submit").click( 

function(){ 

    var mobtel=$("#mobtel").val(); 

    $.ajax({ 
    type: "POST", 
    url: "post_action", 
    dataType: "json", 
    data: "mob_tel="+mobtel, 
    cache:false, 
    success: function (data) { 

      if(data.success == 'yes') { 
        $("#form_message").html(data.message).fadeIn('slow'); 
      } 
      else{ 

        if(data.message == "Please does not match"){ 
        //show error message where ever you want 
         $("#form_message").html(data.message).fadeIn('slow'); 
        }else{ 
        //Do whatever you want to do :: to redirect can use window.location 
         alert('data.message') 
        } 

      } 
    } 

    }); 
    }); 
}); 
+1

我不知道CI,但在某些情況下,你也會需要呼應的AJAX調用的響應之前清理輸出緩衝。這可以通過使用ob_clean()來完成。 – Sparky 2012-01-30 12:28:11

+0

如果在javascript或php中顯示任何錯誤?請讓我知道錯誤..使用錯誤控制檯.. – 2012-01-30 12:29:43

0

你可以使用一個for循環來搜索回調對象

任何鍵如果你想看看一個錯誤鍵回調存在,你可以使用:

success: function(cb) 
{ 
    for(errors in cb) 
    { 
     //checking for error key in the callback 
    } 
}