dpp34603 2014-11-08 03:43
浏览 36
已采纳

CodeIgniter中的AJAX加载页眉和页脚源代码

first of all sorry if my english isn't perfect, but i think you will understand my point.

What i want to do:

Simple web with this template order:

  • header.php
  • nav.php
  • {dynamic content by ajax}
  • footer.php

The problem:

At first page load all the source code works perfect (the header, the nav, the content and the footer) but when i use the menu to load another content using ajax the header, nav and the footer dissapear only from the source code.

I know why this is happening but i don't know how to solve it, take a look at the code below

AJAX menu

$(function(){
    $("a[rel='tab']").click(function(e){
        e.preventDefault(); 

        $("#ajax-content").css("display", "none");
        $("#loading").css("display", "block");

        pageurl = $(this).attr('href');

        $("#ajax-content").load(pageurl, function(){
            $("#loading").css("display", "none");
            $("#ajax-content").css("display", "block");
        });

        $('a').removeClass('active');
        $(this).addClass('active');

        if(pageurl!=window.location){
            window.history.pushState({path:pageurl},'',pageurl);    
        }

        return false;  
    });
});

application/libraries/Showcontent.php

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

class Showcontent {

   function __construct() {
    $this->CI =& get_instance();
   }


   function load_content($current_page) {
        $parts = array('header', 'nav', $current_page, 'footer');
        foreach($parts as $part) {
            $this->CI->load->view($part);
        } 
   }    
}
?>

application/config/autoload.php

$autoload['libraries'] = array('showcontent');

controller example in application/controllers/info.php

<?php
class Info extends CI_Controller {

    public function __construct() {
        parent::__construct(); 
    } 

    function index() {
        if($this->input->is_ajax_request()) {
            $this->load->view('info');
        } else {
            $this->showcontent->load_content('info');
        } 
    }

}
?>

view example in application/views/info.php

<div class="w95 center">
    <h2 class="header">Info Title</h2>
    <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque
    audantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto
    beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut
    odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. 
    Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, 
    sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat 
    Ut  enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit 
    laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit 
    qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum 
    fugiat quo voluptas nulla pariatur?</p>
</div>

So when i go to http://localhost then i click the info link to http://localhost/info i can see the header, nav, info content and the footer, good. But when i try to see the source code of the web i only see the same as the above code, only the info.php view

Thanks for your time!

  • 写回答

1条回答 默认 最新

  • drh96824 2014-11-26 18:44
    关注

    This is a very common problem that we come across with. Always load the view part only when using ajax in your controller function. Check your controller code you must be loading the header.php and footer.php along with the view that is loaded on AJAX request.

    function load_content($current_page) {
            $parts = array($current_page);
            foreach($parts as $part) {
                $this->CI->load->view($part);
            } 
       }    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目