codeigniter视图模板jQuery的AJAX [英] codeigniter view templates jquery ajax

查看:177
本文介绍了codeigniter视图模板jQuery的AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我用codeigniter和使用基本模板,我的网站,所以我的头是在一个单独的文件名为header.php文件和页脚文件footer.php。主要的网站页面变量称为日程地址搜索Maincontent所以在我maincontent.php我想有下面几行:

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');
?>

一切工作正常,直到我想要做某种形式的基本阿贾克斯。我想从一个文件中加载一个页面片段,并在当前加载的页面插入和目标#invoiceforms的ID。 我如何获得Ajax和jQuery来加载只是我需要的片段,并将它注入到当前的目标分区,而无需再次加载页眉和页脚和所有。我想用jQuery的负荷,所以我会做这样的事情:

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});

我需要建立在我的控制器功能指向这个特定的页面,如果是的话怎么那么我挑HTML我需要并注入到当前页面?我有这个问题之前,但不知道怎么问我的问题,所以我只是回避它。我将不胜感激,如果我能在这里帮助。

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.

这是一部分,我不明白。我有这个jQuery的提取HTML表格从页面:

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>');
            });

但是当它出现在目标的div我想在表中出现,那只能说明我的表的文本,但不表结构本身。但是用萤火虫,页面被发送过包括表的标记。可能是什么问题就在这里?

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?

Ĵ

推荐答案

我使用一个单独的控制器,只为那基本上只是输出HTML我要插入到容器元素的AJAX功能。

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

例如,填充级联选择,我jQuery是:

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 */

和我的看法

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

另外,不要忘了 CSRF令牌在您的要求。

这篇关于codeigniter视图模板jQuery的AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆