loading a view after an ajax call, CodeIgniter loading a view after an ajax call, CodeIgniter codeigniter codeigniter

loading a view after an ajax call, CodeIgniter


Ok here is what you are doing wrong.

when you request the page using ajax it does not return you that page.

When you use $this->load->view('pagename',$datapassed); it loads the view and that's why you see nothing.

What you have to do is use

$data=$this->load->view('pagename',$datapassed, TRUE);

What it will do is, it will return that page and save it in $dataafter that you can print it using

$this->set_output($data); 

and receive this in ajax result and load it in a div.

and if you want to refresh the whole page you can use

$(body).html(result);

You have to understand that you need to send the html page by supplying that third parameter in load view.


In controller class

function get_view_ajax(){   $data['username] = $_POST['username];   $response = $this->load->view('radius/radius_corporate_graph',$data,TRUE);   echo $response;}

In view file where you initiate ajax call

$('#button').click(function(){var username = $('#username').val();$.ajax({   type:'POST',   url:"<?php echo base_url(); ?>controller_name/get_view_ajax/",   data: "username="+username,   success:function(msg){    $("#div_result").html(msg);   },   error: function(result)   {      $("#div_result").html("Error");    },   fail:(function(status) {      $("#div_result").html("Fail");   }),   beforeSend:function(d){    $('#div_result').html("<center><strong style='color:red'>Please Wait...<br><img height='25' width='120' src='<?php echo base_url();?>img/ajax-loader.gif' /></strong></center>");   }  }); });<div id="div_result"><a href="#" id="button">Click here </a>  

Another view file to be loaded on controller function (extra_info.php) as refered on get_view_ajax function

<h1>This page is called from ajax function </h1>


Keep in mind dataType must be "html"

$.ajax({        type: 'POST',        url: '<?php echo base_url(); ?>/controllerName/functionName',        dataType: "html",        success: function (response) {                   $("#oh").html(response);                },        error: function (error_) {                    MYLOG(error_);                }       });