[英]Passing null value by ajax to controller
I am trying to modify my save routine to pass a null
value to id_cellcarrier
on to my controller in codeigniter. 我正在尝试修改保存例程,以将
null
值传递给id_cellcarrier
到codeigniter中的控制器上。 The following is what I have tried and it does not work. 以下是我尝试过的方法,它不起作用。 There are a lot of pieces of this code that I am not posting here for brevity sake.
为了简洁起见,我没有在此发布很多代码。 I hope enough is here that you can get the idea of where I am going.
我希望这里足够让您了解我要去的地方。 Do I have to do something on the controller side to understand null?
我是否需要在控制器端执行某些操作才能了解null?
Javascript: Javascript:
/**
* Event: Save Add/Edit Customer Operation "Click"
*/
$('#save-customer').click(function() {
var customer = {
if ($('#cell-carrier').val()== ''){
JSON.stringify({id_cellcarrier: null}),
}else{
id_cellcarrier: $('#cell-carrier').val(),
}
wp_id: $('#wp-id').val(),
notifications: $('#client-notifications').val(),
first_name: $('#first-name').val(),
last_name: $('#last-name').val(),
email: $('#email').val(),
phone_number: $('#phone-number').val(),
address: $('#address').val(),
city: $('#city').val(),
zip_code: $('#zip-code').val(),
notes: $('#notes').val()
};
if ($('#customer-id').val() != '') {
customer.id = $('#customer-id').val();
}
if (!instance.validate(customer)) return;
instance.save(customer);
});
/**
* Save a customer record to the database (via ajax post).
*
* @param {Object} customer Contains the customer data.
*/
CustomersHelper.prototype.save = function(customer) {
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_customer';
var postData = {
csrfToken: GlobalVariables.csrfToken,
customer: JSON.stringify(customer)
};
$.post(postUrl, postData, function(response) {
if (!GeneralFunctions.handleAjaxExceptions(response)) {
return;
}
Backend.displayNotification(EALang['customer_saved']);
this.resetForm();
$('#filter-customers .key').val('');
this.filter('', response.id, true);
}.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler);
};
Controller: 控制器:
/**
* [AJAX] Save (insert or update) a customer record.
*
* @param array $_POST['customer'] JSON encoded array that contains the customer's data.
*/
public function ajax_save_customer() {
try {
$this->load->model('customers_model');
$customer = json_decode($_POST['customer'], true);
$REQUIRED_PRIV = (!isset($customer['id']))
? $this->privileges[PRIV_CUSTOMERS]['add']
: $this->privileges[PRIV_CUSTOMERS]['edit'];
if ($REQUIRED_PRIV == FALSE) {
throw new Exception('You do not have the required privileges for this task.');
}
$customer_id = $this->customers_model->add($customer);
echo json_encode(array(
'status' => AJAX_SUCCESS,
'id' => $customer_id
));
} catch(Exception $exc) {
echo json_encode(array(
'exceptions' => array(exceptionToJavaScript($exc))
));
}
}
From the Model 从模型
public function add($customer) {
// Validate the customer data before doing anything.
$this->validate($customer);
// :: CHECK IF CUSTOMER ALREADY EXIST (FROM EMAIL).
if ($this->exists($customer) && !isset($customer['id'])) {
// Find the customer id from the database.
$customer['id'] = $this->find_record_id($customer);
}
// :: INSERT OR UPDATE CUSTOMER RECORD
if (!isset($customer['id'])) {
$customer['id'] = $this->_insert($customer);
} else {
$this->_update($customer);
}
return $customer['id'];
}
protected function _update($customer) {
// Do not update empty string values.
foreach ($customer as $key => $value) {
if ($value === '')
unset($customer[$key]);
}
$this->db->where('id', $customer['id']);
if (!$this->db->update('ea_users', $customer)) {
throw new Exception('Could not update customer to the database.');
}
return intval($customer['id']);
}
At first glance it seems this portion of your code must be a typo: 乍看起来,您的代码的这一部分一定是拼写错误:
if ($('#cell-carrier').val()== ''){
JSON.stringify({id_cellcarrier: null}),
}else{
id_cellcarrier: $('#cell-carrier').val(),
}
,but if it is not a typo, it should be this: ,但如果不是错字,应该是这样的:
id_cellcarrier: $('#cell-carrier').val() === ''? null: $('#cell-carrier').val(),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.