EllisLab text mark
Advanced Search
     
Views, JSON, and HTML
Posted: 09 November 2012 02:41 PM   [ Ignore ]
Joined: 2012-11-09
9 posts

I’m new to CodeIgniter and MVC.  I’m having some trouble trying to figure out the best place to put a snippet of HTML.  I know that the answer is probably in the View area.  Here is the problem, sometimes I want the snippet of HTML to be called normally, but other times I need AJAX to call it and to return it as JSON.  How do I accomplish this?

On my webpage I have a “Add” button.  This calls the Ajax and should return with some JSON that contains the HTML and the ID of the HTML.

This seems like this should be a common thing that people want to do. How do I accomplish this?

 
Posted: 09 November 2012 06:06 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3796 posts

Lots of ways really, but you could just have a method in your controller that you call with your ajax request and have it return that view.

function send_view($view '')
{
  
if ($this->input->is_ajax_request() && $view !== '')
  
{
    $this
->load->view($view);
  
}

Then send your ajax request to
http://yoursite.com/your_controller/send_view/view_name

If you need to send extra parameters you can do that using POST ajax method and enter them there in the data portion and retrieve them with $this->input->post($field_name) in your “view_name” method where you can do what you need to do and pass them to the load::view()

You could also send them as extra URL segments (instead of data in post) and have it automatically passed to the send_view() method like the example shows with $view.

 Signature 
 
Posted: 09 November 2012 06:58 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2012-11-09
9 posts

The problem is that:

$this->load->view($view);

just dumps the HTML to the screen.  I need to put it in a JSON object and then download it.  So something like this:

{‘html’: ‘<h1>html here</h1>’, ‘id’: 44}

I want to reuse code here.  Sometimes I just want to return the straight HTML and other times I want to put it in JSON and return it. 

How?

 
Posted: 09 November 2012 07:01 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3796 posts
function send_view($view '')
{

  
if ($this->input->is_ajax_request())
  
{
    $data[
'view_html'$this->load->view($view, array(), TRUE);
    echo 
json_encode($data);
    exit;
  
}
  
else
  
{
    $this
->load->view($view);
  
}
 Signature 
 
Posted: 09 November 2012 08:14 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2012-11-09
9 posts

AWESOME!  Did not know there was a third param for returning the HTML in the “view” function.

Thank you so much!

 
Posted: 09 November 2012 11:48 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2011-02-23
882 posts
luster - 09 November 2012 08:14 PM

AWESOME!  Did not know there was a third param for returning the HTML in the “view” function.

Thank you so much!

But the user’s guide says so. Hence reading the guide may really be a pain in the you-know-where but it will save you some “bad experiences” wink

 Signature 

ignited Community Framework (WiP)  |  Read the User’s Guide. It won’t bite.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

CI example .htaccess