json - How to pass back html and logic information after an ajax call with CI -


i have ci , jquery based project. i've got site searching db. consists of jqueryui accordion. 1 section contains input fields advanced search , other section used display html table results.

the search parameters first section sent server using ajax post. crunched server , either html styled error message or html table results (and later other stuff such how many results found, how time consumed etc.) returned.

back on client jquery must able distinguish between two. best able transmit variable 'search_success'. if 'search_success' false, error prepended section 1 above input fields. otherwise html block displayed in section 2 , jquery opens section 2.

right i'm returning plain html 0 or 1 prepended. first char chopped off jquery , used distinguish between 2 possible results. kind of ugly.

after reading post about sending array using json thought addressing problem in json. intended build

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

this alow nice structuring of data. problem is, 'html' not simple php variable 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 lot more complicated. of course abandon ci mvc , store whole html in php string transform json above code. however, defeat purpose of storing whole html part in view.

is there way wrap whole view in json without relinquishing view architecture? or approach more suitable problem?

thanks, singultus

to bring topic end, answer simple:

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

see here @ end. approach allows nicely structured response server.

all credit @koala_dev!


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -