Welcome Guest, Not a member yet? Register   Sign In
view location for ajax forms
#4

I would copy/paste a real fragment of code with additional comments, it is not for clean CodeIgniter, but gives an idea:

Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed.');

class Search_ajax_controller extends Base_Ajax_Controller {

    public function __construct() {

        parent::__construct();
    }

    public function index() {

        $this->output->set_header('Content-Type: application/json; charset=utf-8');

        $this->load->model('sp_search');

        $post = $this->input->post(); // Search parameters.

        $params = array();

        // Remove empty search parameters.
        foreach ($post as $key => $value) {

            if (!empty($value) || $value != '') {
                $params[$key] = $value;
            }
        }

        $url = http_build_url(service_url(), array('query' => $params));
        $data = $this->sp_search->get($params);

        $this->output->set_output(json_encode(array(
            'html' => $this->load->view('service_results', $data, true, 'i18n'), // HTML result.
            'url' => http_build_url(service_url(), array('query' => $params)),  // Search URL for modifying browser's history.
            'total_rows' => $data['total_rows'], // Data to be shown outside the returned HTML-fragment.
        )));
    }

}

The controller responses in JSON format and it returns combined data and html.
Reply


Messages In This Thread
view location for ajax forms - by lloydie-t - 01-19-2015, 08:41 AM
RE: view location for ajax forms - by CroNiX - 01-21-2015, 01:54 PM
RE: view location for ajax forms - by sneakyimp - 01-22-2015, 02:03 PM
RE: view location for ajax forms - by ivantcholakov - 01-22-2015, 05:20 PM



Theme © iAndrew 2016 - Forum software by © MyBB