// James Doughty
// Script for fading user form in and out
// 29 Sept 2015
$(document).ready(function(){
var formEditUser = $('#formEditUser')
editUser = $('#pnlEditUser'),
displayUser = $('#pnlViewUser'),
btnEditUser = $('#btnEditUser'),
btnEditUserSave = $('#btnEditUserSave'),
btnEditUserCancel = $('#btnEditUserCancel'),
alertSuccessSaved = $('#alertSuccessSaved'),
alertUpdateFailed = $('#alertUpdateFailed');
//-----------DIV SHOW HIDE-------------------------------
btnEditUser.click(function() {
alertSuccessSaved.hide(); // turn off messages
alertUpdateFailed.hide(); // turn off messages
displayUser.fadeOut(1000, function() {
editUser.fadeIn(1000, function() {
});
});
});
btnEditUserCancel.click(function() {
editUser.fadeOut(1000, function() {
displayUser.fadeIn(1000, function() {
});
});
});
//------- DIV FORM SUBMIT --------------------------------
formEditUser.submit(function(event) {
$('.help-block').remove(); // remove the error text
$('.form-group').removeClass('has-error'); // remove the error class
var formData = {
'contact_number' : $('input[name=contact_number]').val(),
'first_name' : $('input[name=first_name]').val(),
'last_name' : $('input[name=last_name]').val(),
'password' : $('input[name=password]').val(),
'practice' : $('input[name=practice]').val(),
'address' : $('input[name=address]').val(),
'email' : $('input[name=email]').val(),
'dob' : $('input[name=dob]').val()
};
// process the form
$.ajax({
type : 'POST', // define the type of HTTP verb we want to use (POST for our form)
url : '/ajax_amend_details', // the url where we want to POST
data : formData, // our data object
dataType : 'json', // what type of data do we expect back from the server
encode : true
})
// using the done promise callback
.done(function(data) {
// log data to the console so we can see
console.log(data);
if ( ! data.success) {
// handle errors for email field ---------------
if (data.errors.email) {
// add the error class to show red input:
$('#email-group').addClass('has-error');
// add the actual error message under our input:
$('#email-group').append('<div class="help-bloc col-sm-5 col-sm-offset-2">'
+ data.errors.email + '</div>');
}
// handle errors for password ---------------
if (data.errors.password) {
// add the error class to show red input:
$('#password-group').addClass('has-error');
// add the actual error message under our input:
$('#password-group').append('<div class="help-block col-sm-5 col-sm-offset-2">'
+ data.errors.password + '</div>');
}
// handle errors for email failure ---------------
if (data.errors.sendmail) {
$('#result').html('<div class="alert alert-danger">'
+ data.errors.sendmail + '</div>');
}
}
else {
alertSuccessSaved.show();
// return to view user
btnEditUser.hide();
alertUpdateFailed.hide();
editUser.fadeOut(1000, function() {
displayUser.fadeIn(1000, function() {
});
});
}
})
// using the fail promise callback
.fail(function(data) {
console.log(data);
alertUpdateFailed.show();
});
// NEW FROM RICHARDS CODE
// stop the form from submitting the normal way and refreshing the page
event.preventDefault();
});
//---------------------------------------------------------
});