2014-01-08 90 views
0

我有一個關於我的代碼在ajax的問題​​。警報總是彈出使用ajax

AJAX

<script type="text/javascript"> 
var stopTime =0; 
var scoreCheck = function() 
    { 
     $.ajax({ 
     url: 'http://127.0.0.1/ProgVsProg/main/checkScoreRoundOne', 
     success:function(output){ 
     if(output !=' '){ 
      $('#maincontent1').html(output); 
      bootbox.alert("We have a winner", function(){  
      });  
     stopTime = setTimeout(scoreCheck, 1000); 
      } 
     else { 
      clearTimeout(stopTime); 
      } 
    } 
    }); 
} 
stopTime = setTimeout(scoreCheck,1000); 
</script> 

控制器

public function checkScoreRoundOne(){ 
$id = $this->session->userdata('userID'); 
$battleID = $this->lawmodel->getBattle($id); 
foreach($battleID as $row){ 
    $Rscore = $row->requestedScore; 
    $Cscore = $row->challengerScore; 
    if($Cscore == '1'){ 
     $rID = $this->lawmodel->getID($row->challengerID); 
     foreach($rID as $row){ 
      echo $row->username."Got the correct answer"; 
      } 
     } 
    else if($Rscore == '1'){ 
     $cID =$this->lawmodel->getID($row->requestedID); 
     foreach($cID as $row){ 
      echo $row->username."Got the correct answer"; 
      } 
     } 
    else 
     echo "Answer the question"; 
     } 
} 

我的問題是AJAX會時刻警惕,即使它不符合我的控制器的條件。 我不能測試我的項目,如果它是正確的,或者使用笨not..Im ..

進出口新的阿賈克斯PLSS幫助.. :( 編輯

我怎樣才能使警報彈出只有當它滿足條件在我的控制器?:( 就像當Cscore ==「1」的警報會彈出..

+0

什麼是你想怎麼辦?從你的問題 – Steve

+0

編輯已經不明顯..sorry..im不是很明確.. :( – Zurreal

回答

0

如果我明白你的問題的權利,那麼這行

if(output !=' '){ 

在你的要求應該是類似於

if(output != "Answer the question"){ 

爲什麼?
當您不符合$Cscore == '1'$Rscore == '1'時,返回字符串"Answer the question"。並且知道無論你返回的將是變量的內容,它將永遠不會只是一個空間' '

+0

我甚至嘗試..但它沒有工作.. – Zurreal

0

將foreach代碼移到模型中,而是使用return而不是echo。

說: 型號:

public function getID($row->challengerID){ 
//some code 
if($Cscore || $Rscore){ 
    foreach($cID as $row){ 
     return $row->username."Got the correct answer"; 
     } 
    }else{ 
    return false; 
    }