簡體   English   中英

如何從Codeigniter中的Ajax發布獲取結果查詢

[英]How to get result query from ajax post in codeigniter

我有一個問題如何在codeigniter中獲取結果查詢,我需要獲取結果查詢的值以使用json_encode發送到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..'); 
} 
});
}

我在控制器而非模型中創建函數。 感謝您的幫助和關注。

嘗試這樣,您將需要修改它以適合您的代碼:

<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 ) ;    
    }
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM