简体   繁体   English

如何从Codeigniter中的Ajax发布获取结果查询

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

I have question how to get result query in codeigniter, i need to get the value of result query for send to ajax with json_encode. 我有一个问题如何在codeigniter中获取结果查询,我需要获取结果查询的值以使用json_encode发送到ajax。

The script like this.. 像这样的脚本

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

My Ajax function like this.. 我的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..'); 
} 
});
}

I create the function in controller not model. 我在控制器而非模型中创建函数。 Thanks for the help and attention. 感谢您的帮助和关注。

Try like this, you will need to modify it to suit your code though: 尝试这样,您将需要修改它以适合您的代码:

<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