How does one go about returning a json object in this case(messages) to a view(admincp_index).
The method below works fine but I would really like to spice it up with some animations
Regards, Phil
/* AdmincontrolPanel */
function index()
{
$data['messages'] = $this->session->flashdata('messages');
$data['content'] = 'admincp/admincp_index';
$this->load->view('backend/template', $data);
}
function applicant()
{
$id = $this->input->post('id');
if($this->input->post('accept'))
{
if($this->admincpModel->accept_applicant($id) == TRUE)
{
$this->session->set_flashdata('messages', '<div class="ok">Applicant Added!</div>');
redirect('admincp');
}
}
/* admincp_index */
if($messages){
// echo messages
}
There are three things that need to be kept in mind:
The browser may cache the JSON response, so it’s a good idea to append a time-stamp to the end of the URL to keep the data coming in fresh. (This is true of the GET method, not necessarily so with POST though).
The content type of the JSON response needs to be “application/json” or “text/javascript“.
The
json_encodefunction was included with PHP 5.2, so older environments may not be able to use it, and you’ll have to either get the module installed or write your own encoding class.I’m doing some work on a server running PHP 5.1.6, and I don’t need to serialize any complex types, so I’ve found the technique shown below to work fine. I’m using a simple “JSON view” which sets the correct content type in the response header, and emits a JSON string which was manually concatenated in the controller.
Phil, jQuery effects/animations could make use of the returned JSON data in the success callback function. In the example below I’m just showing message in an alert box.
Client-side Code:
Controller (/application/controllers/page.php):
View (/application/views/json_view.php):