• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ajax tabs

#1
[eluser]crumpet[/eluser]
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.
Code:
$return['view'] = $this->load->view('front/modules/resources/resource_view/additional/comments');
echo json_encode($return);
ends up as this :
Code:
{"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.

Thanks

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

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.
Code:
// php
echo $this->load->view('front/modules/resources/resource_view/additional/comments', '', TRUE);
exit;

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

#3
[eluser]crumpet[/eluser]
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).

#4
[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.

#5
[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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.