Home > Enterprise >  Ajax Parameters are not reaching in CodeIgniter Model
Ajax Parameters are not reaching in CodeIgniter Model

Time:06-14

I am working on load more functionality in CodeIgniter.

The problem is that when I click on Load More button, a request sent to controller using Ajax with parameters. But parameters are not reaching to the CodeIgniter model, but I can print it in controller. Below is code, you can understand problem correctly.

Ajax Request

function loadmore(){
    var pid = $('#last_id').val();
    base_url="<?php  echo base_url(); ?>";
    
    $.ajax({
        type : "post",
        url:base_url "News/loadmore_news",
        data : {'pid' : pid},
        success : function(res){
            for(var i=0;i<res.length;i  ){
                // here is my code
            }
        }
    });
}

Controller

function loadmore_news(){
    $lastid = $this->input->post('pid'); // here when i print this it shows the value which is coming from ajax request.
    $more_news = $this->news_model->get_more_news($lastid);
    echo jsone_encode($more_news);
}

Model

function get_more_news($last_id){
        //echo $last_id;  here when I print it is shows nothing.
        $this->db->select('*')->order_by('id desc');
        $this->db->where(array('id <'=>$last_id,'domain_id'=> config_item('domain_id')));
        $this->db->order_by('id desc');
        $this->db->limit(6);
        $query = $this->db->from('press')->get();
        $getmorenews = $query->result();
        return $getmorenews;
    }

When parameter is not reaching to Model it shows following DB error.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND domain_id = '86' ORDER BY id desc, id desc LIMIT 6' at line 4.

Error in following query is coming because $last_id is not getting value from controller.

SELECT * FROM `press` WHERE `id` < AND `domain_id` = '86' ORDER BY `id` desc, `id` desc LIMIT 6

Question

My question is where I am doing wrong due to which I am not getting variable value in model?

CodePudding user response:

Sorry I can not comment due to lack of reputation. I guess you need to debug the variable. Here is some idea.

  • In your javascript function loadmore, console.log the pid variable.
  • In the controller function just print_r the variable that has contain value or not.

Nothing wrong with your model. Thanks

CodePudding user response:

hello @Hamza Zafeer time bool value just like 0 it's shows nothing try with a static value first of all if it's come then you can try with this

Controller

function loadmore_news(){
$lastid = '123'; // here when i print this it shows the value which is coming from ajax request.
$more_news = $this->news_model->get_more_news($lastid);
echo jsone_encode($more_news);}
  • Related