Welcome Guest, Not a member yet? Register   Sign In
Ajax tabs

Hi, I am trying to create a product view page with tabs for comments, related products, etc. I want these tabs to load through ajax when you click the buttons. My first instinct was to have ajax post to a seperate function for each tab and have that function reply with a view file which the js displays. I can't seem to get this to work with json.
$return['view'] = $this->load->view('front/modules/resources/resource_view/additional/comments');
echo json_encode($return);
ends up as this :
{"view":null}whats up
where "whats up" is what is in the view file. I'm guessing this is becuase its to big. Is there a way around this or is the best practise to have php only send the variables and make js format the data into a view.


[eluser]Pascal Kriete[/eluser]
It's because you're not returning the view file. By default it's added to the CI output 'buffer'.
$return['view'] = $this->load->view('front/modules/resources/resource_view/additional/comments', '', TRUE);
echo json_encode($return);

I don't really see why you have to do this though. If the view is the only thing you're sending, just echo it.
// php
echo $this->load->view('front/modules/resources/resource_view/additional/comments', '', TRUE);

// example js callback
var success = function(o) {
    // Define tab_3 outside this function and check for it before calling the ajax function - sort of like a cache, only request once (unless the data changes)
    tab_3 = o.responseText;
    my_container . innerHTML = tab_3;

I realized that error about 30 seconds after i posted and felt stupid. But thats a good point about the object. I've changed it now to be like how you have suggested.
Can you comment on whether its better to have php assemble the view or js... for instance one tab is called comments so I could have it respond with an array of comments and make jquery format and display them. Right now I have php do the formatting so jquery can just do $('#display').html(response).

[eluser]Pascal Kriete[/eluser]
I would definitely assemble them with PHP. If you decide to change the formating in a year or two it'll be a lot easier to work out what's going on.

[eluser]Colin Williams[/eluser]
As a rule I would almost always have Ajax calls return formatted code that way you can just hop in and edit the view file to change the markup. I would only return JSON if you are meaning to serve requests from external servers, in which case you are probably just returning data straight from a model function.

Theme © iAndrew 2016 - Forum software by © MyBB