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

Is there a recommended format for where ajax view files are kept?

i.e.

view -> customer -> edit.php

or

view -> customer -> ajax -> edit.php

or any other recommendations
Reply
#2

Really just a personal organization preference. There is no right or wrong way.
Reply
#3

It depends on a variety of things. On a site I'm working on, our ajax requests will all be answered with JSON-encoded objects -- no HTML at all. That being the case, we don't need any AJAX views.

If you are outputting HTML in response to your AJAX requests, I can think of two ways to approach organization of hte views:
1) organize by general function as in both of your examples above.
2) for some AJAX functionality, it may apply in many ways across your site. E.g., a view that corresponds to a 3-column table might be used by every controller on your site. In this case, you might want to organize AJAX according to what kind of output structure it defines. E.g., data table, image with caption, etc.
Reply
#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




Theme © iAndrew 2016 - Forum software by © MyBB