$ret; // declare a variable to return.
if (!$this->ion_auth->logged_in()){
if($this->input->is_ajax_request()){
/* This is an AJAX request, send the url back to be redirected to. */
$ret['url'] = site_url().'auth/login';
$ret['html'] = FALSE;
}else{
/* This is not ajax, it is a standard form submission, let codeigniter handle it*/
redirect('auth/login');
}
} else {
if($this->input->is_ajax_request()){
$ret['url'] = FALSE; //they are logged in, and this is ajax, don't redirect.
$ret['html'] = $this->load->view('modal/productdetail', '', TRUE); //allow the HTML to be returned to the server
}else{
/* They are logged in, but this is not ajax, redirect them. */
redirect('modal/productdetail');
}
}
echo json_encode($ret); //json_encode our $ret array, and send to browser
With the above, we're creating a variable called $ret
that holds reference to some data when performing ajax requests. Otherwise, when not using ajax (no javascript), standard form redirection behavior ensues.
Now, in our success function, we're going to check for the existence of the data.url
and data.html
properties and act accordingly.
dataType: 'json', //add this so the success: knows how to parse returned data
success:function(data){
if(data.url){
window.location = data.url;
}else if(data.html){
$('element').html(data.html); // where "element" is an HTML element on your page you wish to override with a partial view
}
}