0

I have a CI and jQuery based project. I've got a site searching my db. It consists of a jQueryUI accordion. One section contains input fields for an advanced search and the other section is used to display a html table with results.

The search parameters from the first section are sent to the server using ajax post. This is crunched by the server and either a html styled error message or a html table with results (and later some other stuff such as how many results found, how much time consumed etc.) is returned.

Back on the client jQuery must be able to distinguish between the two. Best would be to be able to transmit another variable 'search_success'. If 'search_success' is false, the error is prepended to section one above the input fields. Otherwise the html block is displayed in section two and jQuery opens section 2.

Right now I'm returning plain html with a 0 or 1 prepended. This first char is chopped off by jQuery and used to distinguish between the two possible results. This is kind of ugly.

After reading this post about sending array using json I thought about addressing this problem in json. I intended to build something like

echo json_encode(array('search_success' => $search_success, 'html' => $html));

This would alow for nice structuring of the data. Problem now is, my 'html' is not a simple php variable but a view:

<?php 
$template = array('table_open' => '<table id="table" data-url="'.base_url().'">');
$this->table->set_template($template);
$this->table->set_heading($table_header);
echo $this->table->generate($table);
?>

This view could also get a lot more complicated. Of course I could abandon the CI MVC and store the whole html in a php string which I could transform to json with the above code. However, this would defeat the purpose of storing the whole html part in a view.

Is there a way to wrap my whole view in json without relinquishing my view architecture? Or what approach would be more suitable to the problem?

Thanks, singultus

Community
  • 1
  • 1
singultus
  • 25
  • 1
  • 7
  • Your question is a little vague - what kind of logic do you need to process, and what would a sample html data be from the server? – Terry Oct 06 '13 at 19:44
  • I hope the edit clarifies the problem enough. – singultus Oct 06 '13 at 20:20
  • have you tried returning the view as data in your controller using the third parameter in $this->load->view() as `$html = $this->load->view('myfile', '', true);` this is documented [here](http://ellislab.com/codeigniter/user-guide/general/views.html) at the very end – omma2289 Oct 06 '13 at 20:40
  • Doh, that easy! I wasn't aware of this tiny detail. Solved the whole issue. If you submit it as an answer I'll accept it. – singultus Oct 06 '13 at 20:58

1 Answers1

0

To bring this topic to an end, the answer is simple:

$json['html'] = $this->load->view('myfile', '', true); // 3. param 'true'!
$json['other_stuff'] = $other stuff;
echo json_encode($json);

See here at the very end. This approach allows for a nicely structured response to the server.

All credit to @koala_dev!

singultus
  • 25
  • 1
  • 7