Michael Grigsby
Michael Grigsby

Reputation: 12163

jQuery not properly loading a CodeIgniter view

$("#slideshow").click(function() {
    $("#content").fadeOut(function() {
        $(this).load('<?php $this->load->view("slideshow"); ?>', function() {
            $(this).fadeIn()
        });
    });
});

When I run the above code in Ci, CodeIgniter pends the 'slideshow' view to the beginning of the document. I need it to only display when the id of 'slideshow' is clicked. This code snippet usually works, but idk why it's acting funny now. Any ideas?

Upvotes: 0

Views: 1775

Answers (3)

hakre
hakre

Reputation: 197787

This produces such a garbage that your browser will put it somewhere (at the beginning of the document):

$(this).load('<?php $this->load->view("slideshow"); ?>', function() {

Because the string needs proper encoding, e.g. as a javascript string with json_encode­Docs:

$(this).html(<?php echo json_encode($view_html); ?>);
$(this).fadeIn(); 

Check the CI docs how you get the view returned as string instead of being outputted. I leave this as an exercise.

Upvotes: 1

gen_Eric
gen_Eric

Reputation: 227270

I'm not sure what you're trying to do here. View files in CodeIgniter are supposed to contain HTML. The Controllers can load the views, and pass them data if need be. Views are templates.

$(this).load('<div id="test"></div>', function(){});

This will throw an error, as .load expects a URL, not HTML (unless you're binding to the onload event, where the 1st param can be an array of data, but I don't think that's what you're doing).

What I assume you want is to have jQuery load the content of the view file into #content. To do this, you're gonna need a controller that outputs the view. For example:

Controller:

function slideshow(){
    $this->load->view('slideshow');
}

jQuery:

$("#slideshow").click(function() {
   $("#content").fadeOut(function() {
      $(this).load('/path/to/controller/slideshow', function() {
         $(this).fadeIn()
      });
   });
});

Upvotes: 3

chenio
chenio

Reputation: 592

try do it:

$(this).load("<?php $this->load->view('slideshow','','true'); ?>", function() { ......  }

Note that i use single quote, and third parameter true.

Upvotes: 0

Related Questions