[eluser]Jay Turley[/eluser]
Okay, here is my property controller (abridged):
Code:
class Property extends Controller {
function Property() {
parent::Controller();
// load class libraries
$this->load->library(array('session'));
$this->load->database();
}
function create() {
$this->load->helper('url');
$this->load->model('property_model');
$new_name = $this->input->post('new_name',TRUE);
$new_lat = $this->input->post('new_lat',TRUE);
$new_lng = $this->input->post('new_lng',TRUE);
if (!$new_name || !$new_lat || !$new_lng) {
$data['message'] = 'ERR';
$this->load->view('new_property_fail', $data);
}
else {
$propertyInfo = array(
'name' => $new_name,
'latitude' => $new_lat,
'longitude' => $new_lng,
'private' => 0,
'user_id' => (int) $this->session->userdata('user_id'),
'created_on' => date('Y-m-d'),
'updated_on' => date('Y-m-d')
);
// make insertion, grab insert_id
if ($this->property_model->create($propertyInfo)) {
$data['propertyName'] = $new_name;
$this->load->view('new_property_success', $data);
} else {
$data['message'] = 'ERR';
$this->load->view('new_property_fail', $data);
}
}
}
}
And here is the AJAX which sends it post information (i'm using jQuery):
Code:
function storeNewProperty() {
// serialize() requires inputs have 'name' attribute or else won't work;
var newPropertyData = jQuery('#new_property').serialize();
jQuery.ajax({
type: "POST",
url: "/index.php/property/create",
data: newPropertyData,
dataType: "text",
success: function(data,textStatus){
if (data == "ERR") {
jQuery('.loader_image').remove();
jQuery('#new_property').show();
} else {
// do some success stuff
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
if (textStatus == 'timeout') {
alert("It appears we are having network issues. Please try again.");
jQuery('.loader_image').remove();
jQuery('#new_property').show();
} else {
alert('Problem saving property: ' + textStatus + ' - ' + errorThrown);
}
}
});
return false;
}
Please note, I have chopped this down. A lot of the error checking is not included. But the essence is that the javascript sends the POST request to the Property.Create() method, receives a message back, and then does either the error or success callback.