简体   繁体   中英

AJAX POST not working to load div view in CODEIGNITER

Help me out with my ajax thing...Alright what i am trying to do is i have a side column navigation panel in may main view..all the navigation links is fetched from database.now what i want is when i click any link it should load the CI view div in the same main view but in the middle panel and if click second link it should replace the old view div with new one without loading page.The Divs to be loaded after click are placed in the views folder of the project(CI views folder). Now what i have tried is when link is clicked i call the ajax with the view name passes as the data to the URL(/controller/method) and controller load the view and on ajax success the view should be loaded in the middle panel as the new div.

View Code:

script code:

<script type="text/javascript">

        $(document).ready(function(){

        $("a.links").click(function(e){

        e.preventDefault();
        alert("alert on click");
        var platform_id = $(this).attr('id');
        alert(platform_id);


        $.ajax({

             url: '<?php echo base_url() ?>admin/load_view', 
             type: 'POST',
             datatype:'json',
             data: {'view_param': platform_id},
             success: function(response)
             {
                 if (response.success == 1)
                    {
                       alert('success');
                    }
                    else{
                    alert('unseccessful');   //just a alert to see if response is successful              
                    }       
             },
             error: function(response){
                    alert(response.message);
                }

        }); 


        })
        })

        </script>


<div id="left-column">
          <h3>Header</h3>
          <ul class="nav">
         <?php foreach($ops as $operations)
            {$op_name = $operations['admin_op'];
             $op_link = $operations['link_to'];
        ?>  

        <li><a href="" id="<?php echo $op_name ; ?>" class="links"><?php echo $op_name ;?></a></li>
            <?php } ?>
      </ul>
          <a href="http://all-free-download.com/free-website-templates/" class="link">Link here</a> <a href="http://all-free-download.com/free-website-templates/" class="link">Link here</a> 
          </div>

middle panel div:

<div id="mang_server"> </div>

Controller code:

public function load_view(){

$view_name = $this->input->post('view_param');
log_message('var dump', var_export($view_name));
$data['view_link'] = $this->admin_model->load_view_model($view_name);
$response['html_view'] = $this->load->view($data['view_link'],'',TRUE);
echo json_encode($response);
//$this->load->view($data);
}

I tried to debug it also i observe that it goes success portion of the ajax but it alerts the else part of it.

error portion alert:

'Manage Servers'{"success":1,"message":"Some success message","html_view":"<table class=\"listing form\" cellpadding=\"0\" cellspacing=\"0\">\n          <tr>\n            <th class=\"full\" colspan=\"2\">Header Here<\/th>\n          <\/tr>\n          <tr>\n            <td class=\"first\" width=\"172\"><strong>Lorem Ipsum<\/strong><\/td>\n            <td class=\"last\"><input type=\"text\" class=\"text\" \/><\/td>\n          <\/tr>\n          <tr class=\"bg\">\n            <td class=\"first\"><strong>Lorem Ipsum<\/strong><\/td>\n            <td class=\"last\"><input type=\"text\" class=\"text\" \/><\/td>\n          <\/tr>\n          <tr>\n            <td class=\"first\"><strong>Lorem Ipsum<\/strong><\/td>\n            <td class=\"last\"><input type=\"text\" class=\"text\" \/><\/td>\n          <\/tr>\n          <tr class=\"bg\">\n            <td class=\"first\"><strong>Lorem Ipsum<\/strong><\/td>\n            <td class=\"last\"><input type=\"text\" class=\"text\" \/><\/td>\n          <\/tr>\n        <\/table>\n        <p>&nbsp;<\/p>"}

in your controller you didn't assigne $response['success'] and $response['message'] which you need in the front end. Change the controller code into,

public function load_view()
{
    $view_name = $this->input->post('view_param');
    log_message('var dump', var_export($view_name, true));
    $data['view_link'] = $this->admin_model->load_view_model($view_name);

    $response['success'] = 1;
    $response['message'] = "Some success message";
    $response['html_view'] = $this->load->view($data['view_link'],'',TRUE);
    echo json_encode($response);
    exit;
}

Hope this helps :)

Updated:

NOTE: in log_message('var dump', var_export($view_name, true)); , var_export() need a second parameter to return the value. Otherwise, it will output a text and causes problem with your ajax response.

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