codeigniter view templates jquery ajax

2019-04-16 18:37发布

问题:

my question is i use codeigniter and use basic templating for my site so my header would be in a separate file called header.php and footer in file footer.php. The main site pages are in variables called maincontent so in my maincontent.php i would have the following lines:

<?php
  $this->load->view('includes/header');
  $this->load->view($maincontent);
  $this->load->view('includes/footer');
?>

everything works ok until I want to do some form of basic ajax. I want to load a page segment from a file and insert it in a currently loaded page and target id of #invoiceforms. How do i get ajax and jquery to load just the segment I need and inject it into the current targeted div without loading the header and footer again and all that. I thought of using jquery load so I would do something like this:

$('#invoiceforms').load('path/to/page');
//or
$.get('path/to/page',function(){etc});

do I need to create a function in my controller to point to this particular page and if so how then do I pick the html I need and inject it into the current page? I had this problem before but didn't know how to ask my question so I just sidestepped it. I would be grateful if I could be aided here.

This is the part I don't get. I have this jquery that fetches the html table from a page:

$.get('../ajaxops/loadinvoice',function(data){
                $('#invoiceform').html('<table>'+data+'</table>');
            });

but when it shows up in the target div that I would like the table to appear in, it only shows me the text of the table but not the table structure itself. But using firebug, the page gets sent over including the table's markup. what could be the problem here?

j

回答1:

I use a separate controller just for ajax functions that basically just outputs the html I'm going to insert into the container element.

For example, to populate a cascading select, my jquery is:

$(".addform #country, .searchform #country").change(function () {
        var p = $("#country option:selected").val();
        $('#state').load('/ajax/states/get', {'country_id': p, 'csrf_token_name': cct}, function (data) {this.value = data;})
;
}).change();

my controller function

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class States extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
    }

        // populate state dropdown via AJAX, based on country selected
        function get() {
                $this->form_validation->set_rules('country_id', 'country', 'trim|required|strip_tags|is_natural_no_zero');
                $data = null;

                if($this->form_validation->run() == TRUE ) {
                        $cid = $this->input->post('country_id');

                        $this->load->model('Mgeography');
                        $data['arr'] = $this->Mgeography->get_all_state_by_country_id($cid);

                        $data['optype'] = "select";
                        $data['label'] = "state";
                        $data['key'] = "state";
                        $this->load->view('ajax_view',$data);
                }
        }
}
/* End of file states.php */
/* Location: ./application/controllers/ajax/states.php */

and my view

if ($optype == "select") {
    echo "<option value=\"0\">Choose ${label}</option>";
    foreach($arr as $row) {
        echo "<option value=\"" . $row['id'] . "\">" . $row[$key] . "</option>";
    }
}

also, don't forget about the CSRF token in your requests.