1

Here is my little script code I want to get data from codeingiter controller. I get json data from controller to view ajax, but It print with html page code. any one can help me here, How can I solve this.

I only want to get json data ans a variable data to my page.

this is output that I am getting but this is comming with html code and I don't want html code.

[{"id":"1","p_name":"t_t11","p_type":"t_t1","paid_type":"0"},{"id":"2","p_name":"t_t12","p_type":"t_t1","paid_type":"1"},{"id":"3","p_name":"t_t1","p_type":"t_t1","paid_type":"0"}]

I have follow some question answers but can't et success, because that question's answers not related to me.

Link 1
Link 2 and many more...

<script>
   $("a.tablinks").on('click',function(e){
        e.preventDefault();     
        var p_name = $(this).attr('value');
        alert(p_name);
    $.ajax({            
            url:"<?php echo base_url(); ?>teq/gettabdata",                  
            dataType:'text',
            type: "POST",          
            data:{p_name : p_name},        
           success : function(data){
                    alert(data);
                if(data !=""){

                    var obj = JSON.parse(data);
                    alert(obj.id);
                    /*$.each(obj, function(key,val){
                         console.log(key);
                         console.log(val); //depending on your data, you might call val.url or whatever you may have
                    });*/

                }else{
                    alert(data+ '1');
                }
           },
           error : function(data){

                //var da = JSON.parse(data);
                alert(data+ '2');
                //alert(da+ '2 da ');
           }
        });
    });
</script>

Here is controller code.

  public function gettabdata(){

                    $p_name = $this->input->post('p_name');
                    //echo $p_name." this is paper name.!";

                    $tabs_data['res1'] = $this->db->distinct()->select('p_type')->from('t_name')->get()->result();      

                    //$p_name = $data;
                    $query['res'] = $this->db->select('*')->from('t_name')->where('p_type',$p_name)->get()->result();

                    echo json_encode($query['res']);
                    $this->load->view('teq', $tabs_data);


        }
1
  • The javascript is in the view teq? Commented Apr 18, 2017 at 9:36

4 Answers 4

2

You added view at the end of your function that return view's code.

Remove line:

$this->load->view('teq', $tabs_data);
Sign up to request clarification or add additional context in comments.

3 Comments

I have only one controller to load page so i must need it
so you are getting data through gettabdata controller and set also with that using ajax?
you can't do that with single controller because getting data and load view at same time so function return view also
1
You can either use 
if ($this->input->is_ajax_request()) {
       echo json_encode($data_set);
    }else{
//Procced with your load view

}

Or if you're avoiding ajax request check then please pass any extra paramter from your ajax call then then check for its existence at your controller and on behalf of it proceed your conditional statement . it will solve your problem

Comments

0

Change your controller method like this:

public function gettabdata(){
    $p_name = $this->input->post('p_name');
    //echo $p_name." this is paper name.!";
    $tabs_data['res1'] = $this->db->distinct()->select('p_type')->from('t_name')->get()->result();      
    //$p_name = $data;
    $query['res'] = $this->db->select('*')->from('t_name')->where('p_type',$p_name)->get()->result();
    // if ajax request
    if ($this->input->is_ajax_request()) {
       echo json_encode($query['res']);
       return; // exit function
    }

    $this->load->view('teq', $tabs_data);
}

Comments

0

In your ajax code chage dataType: to json

$.ajax({            
    url:"<?php echo base_url(); ?>teq/gettabdata",                  
    dataType:'json',
    type: "POST",          
    data:{p_name : p_name},        
    success : function(res)
    {
        if(res !=""){
            alert(res.id);
        }else{
            alert(res+ '1');
        }
   }
});

And in your controller

public function gettabdata()
{
    if($this->input->post('p_name'))
    {
        $p_name = $this->input->post('p_name');
        $query['res'] = $this->db->select('*')->from('t_name')->where('p_type',$p_name)->get()->result();
        if($query['res'])
        {
            $resp = $query['res'];
        }
        else
        {
            $resp = array('status' => FALSE,'msg' => 'Failed');
        }
        echo json_encode($resp);
    }
    else
    {
        $tabs_data['res1'] = $this->db->distinct()->select('p_type')->from('t_name')->get()->result();
        $this->load->view('teq', $tabs_data);
    }
}

Hope this helps :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.