简体   繁体   中英

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.

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..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM