loading a view after an ajax call, CodeIgniter

折月煮酒 提交于 2019-12-03 04:43:21
Muhammad Nasir

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 thats 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 $data after 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>
mudassar031

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_);
                }
       });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!