Welcome Guest, Not a member yet? Register   Sign In
AJAX AND JSON
#4

[eluser]Krzemo[/eluser]
Some code below from top of my head (no warranty though Wink)
Code:
//in da controller
function blog()
{
    $id = $this->input->post('id');
    if($this->_is_ajax())
    {
        $json = null;
        $json->results = $this->model_blog->get_blog($id);
        $this->output->set_header('Content-type: application/x-json');
        $d['message'] = json_encode($json);
        $this->load->view('admin/vjson_response_wrapper', $d); //this simple view just doing <?=$message?>
    }
    else
    {
      //here you put regular code for loading view if its not ajax (usefull when browser JS is off or it google indexing)
    }
}

//after Jamie
function _is_ajax() {
    return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') ? TRUE : FALSE;
}


and the rest is done by javascript (me like jQuery)
Code:
//this is to be used with an event
        $.post('/admin/blog', {'id': $('input#id').val()},
                function(data){
                    $('input#username').val(data.username);
                    $('input#first_name').val(data.first_name);
                }
            , 'json');


Messages In This Thread
AJAX AND JSON - by El Forum - 12-07-2009, 03:20 PM
AJAX AND JSON - by El Forum - 12-07-2009, 04:57 PM
AJAX AND JSON - by El Forum - 12-08-2009, 12:13 PM
AJAX AND JSON - by El Forum - 12-08-2009, 04:47 PM
AJAX AND JSON - by El Forum - 12-17-2009, 01:17 PM



Theme © iAndrew 2016 - Forum software by © MyBB