2013-08-23 36 views
0

我有問題如何獲取codeigniter中的結果查詢,我需要得到結果查詢的值發送到與json_encode的ajax。如何從代碼中的ajax後得到結果查詢

這樣的腳本..

public function getPost() 
    { 
    $getCode = $_POST['part_code']; 
    $query = $this->db->query('SELECT count(*) + 1 as count FROM TB_TRANSACTION WHERE PART_CODE ='%$getCode%''); 
    foreach ($query->result('TB_TRANSACTION') as $row) 
    { 
     echo $row->count; // call attributes 
    } 
    $phpVar = array("STATUS"=>$row->count); 
    echo json_encode ($status) ;  
    } 

這樣我的Ajax功能..

<script> function makeAjaxCall() 
{ 
$.ajax({ 
     type: "post", 
     url: "http://localhost/IWOS_CI/trans_invent_controller/getPost", 
     cache: false, 
     data: $('#form1').serialize(), 
     success: function(json){  
try{  
     var obj = jQuery.parseJSON(json); 
     var r = obj['STATUS']; 
}catch(e) 
     { 
     alert('Exception while request..'); 
     } 
}, 
     error: function(){ 
     alert('Error while request..'); 
} 
}); 
} 

我創建控制器功能不是模型。 感謝您的幫助和關注。

回答

1

嘗試這樣,你將需要對其進行修改以適應您的代碼,但:

<script type="text/javascript"> 
function makeAjaxCall() 
{ 
    $.ajax({ 
     type: "post", 
     url: "http://localhost/IWOS_CI/trans_invent_controller/getPost", 
     cache: false, 
     data: $('#form1').serialize(), 
     success: function(json){  
      if(json){ 
       var statusR = json.STATUS; 
       alert("status : "+statusR); 
      }else{ 
       alert("Error In JSON"); 
      } 
     }, 
     error: function(){ 
      alert('Error while request..'); 
     } 
    }); 
} 

</script> 
<?php 
    function getPost() 
    { 
     $getCode = $_POST['part_code']; 
     $query  = $this->db->query("SELECT count(*) + 1 as count FROM videos WHERE id = '%$getCode%'"); 
     //echo $this->db->last_query(); die; 
     $queryData = $query->row_array(); 
     $phpVar  = array("STATUS" => $queryData['count']); 
     echo json_encode ($phpVar) ;  
    } 
?> 
+0

嗨,Niloy薩哈我試試你的函數的getPost與阿賈克斯不工作。你可以使用你的getPost函數來調整我的ajax函數。感謝您的關注 – okywijaya

+0

請指明您的錯誤。檢查控制檯是否有錯誤。 –

+0

結果不顯示在警報中,var r = obj ['STATUS'];我在我的功能中創建警報。我認爲錯誤在這裏$ query-> num_rows();. – okywijaya