I am working in Codeigniter. I have used my model in other coding. I know it works.
I am getting AJAX parsererror and Failure Handler at the point of running the model. It seems that JSON syntax is not processed correctly. When I echo back the data for $waitinglist
on the php end I see this in my error message in the console:
"array(2) {↵ ["id_users_provider"]=>↵ string(2) "86"↵ ["notes"]=>↵ string(47) "emailaddress@verizon.net;9093334444@tmomail.net"↵}↵"
It does not look correct. I think it should have something like
"{"appointment":{"id_users_provider":"85","notes":"emailaddress@verizon.net;9093334444@tmomail.net"}}"
So where have I gone wrong?
PHP Controller
public function ajax_register_waiting(){
try {
$post_data = $_POST['post_data'];
$waitinglist = $post_data['appointment'];
$this->load->model('appointments_model');
$this->appointments_model->waitinglist_to_db($waitinglist);
} catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
}
public function book_waiting(){
$this->load->view('appointments/waiting_success');//return to book view
}
JS
$('#save-waitinglist').click(function(event) {
if(validateWaitinglistEmail() && validateWaitinglistPhone()){
postWaiting = new Object();
var note = ''
if($('#cell-carrier2').val() !== "" && $('#phone-number2').val() !== ""){
note = $('#email2').val() + ";" + $('#phone-number2').val().replace(/[^\d\+]/g,"") + $('#cell-carrier2').val();
} else {
note = $('#email2').val();
}
postWaiting['appointment'] = {
'id_users_provider': $('#select-provider').val(),
'notes': note,
};
$('input[name="csrfToken"]').val(GlobalVariables.csrfToken);
$('input[name="post_waiting"]').val(JSON.stringify(postWaiting));
var formData = jQuery.parseJSON($('input[name="post_waiting"]').val());
var postData = {
'csrfToken': GlobalVariables.csrfToken,
'post_data': formData,
};
var postUrl = GlobalVariables.baseUrl + '/index.php/appointments/ajax_register_waiting';
$layer = $('<div/>');
$.ajax({
url: postUrl,
method: 'post',
data: postData,
dataType: 'json',
beforeSend: function(jqxhr, settings) {
$layer
.appendTo('body')
.css({
'background': 'white',
'position': 'fixed',
'top': '0',
'left': '0',
'height': '100vh',
'width': '100vw',
'opacity': '0.5'
});
}
})
.done(function(response) {
if (!GeneralFunctions.handleAjaxExceptions(response)) {
return false;
}
window.location.replace(GlobalVariables.baseUrl
+ '/index.php/appointments/book_waiting');
})
.fail(function(jqxhr, textStatus, errorThrown) {
GeneralFunctions.ajaxFailureHandler(jqxhr, textStatus, errorThrown);
})
.always(function() {
$layer.remove();
})
}
});