I have build simple function which helps me to make my code nice and clean as well as to have general solution for “on_change” AJAX submit. I have named the function q_submit_ajax_on_change with q in the front because I use it in my quantify project. The function uses another pre-build function called q_submit_ajax_on_change which I will describe next. You called this function from anywhere you have your form which should be submitted via AJAX. You need to pass the element (input or textarea) which will be controlled by this function. The function basically checks the value of the element and if the value has changed then the data are submitted via AJAX on blur, which means when the user leaves the element.
function q_submit_ajax_on_change ($element, $form){
$($element).live('focus', function(){
$characters = $(this).attr('value');
});
$($element).live('blur', function(){
$current_characters = $(this).attr('value');
if($characters == $current_characters){
// No change ....
}else{
// Submit the Form when the user looses focus
q_ajax_form_submit($form, $('#system_message'));
}
});
}
The data are submitted using the q_ajax_form_submit function which I have also build for quantify project. It requires 2 variables, form and the id of the element where the success or failure message should be displayed. This function uses the basic jQuery Ajax function but on top displays the progress bar (handled by q_display_progress_bar() function and displays custom success and failure messages, using functions – q_display_success_message() and q_display_failure_message().
function q_ajax_form_submit (form, message_box){
q_display_progress_bar();
$form = form
$url = $form.attr('action');
$dataString = $form.serialize();
$.ajax({
type: "POST",
url: $url,
data: $dataString,
success: function (data) {
q_display_success_message();
// Return the data as well
$(message_box).html(data);
},
error: function () {
q_display_failure_message();
}
});
}
This is my basic framework for the AJAX calls. Here are the last three functions mentioned before.
Function q_display_progress_bar basically inserts animated gift which is specified as background css image for ajax_loader id. You can see this loader is inserted before id called content. We could also specify variable instead of id directly as I have done in my case.
Function q_display_success_message is pretty specific for my case where I have decided to insert the “Saved” image into the page and fade it out. You can see that I remove the animated loader from the page before I insert the “saved” image.
The q_display_failure_message is doing basically the same what the success function but is using one more additional function which scrolls the page to the top. q_scroll_top(110,250) as well as inserts the black overlay using function q_insert_overlay(’body’).
function q_display_progress_bar (){
// Display animation during the ajax call
$('<span id="ajax_loader"></span>').insertBefore('#content');
}
function q_display_success_message (){
// Display "Saved" message when ajax returns success
$('#ajax_loader').remove();
$('<span class="saved">Saved</span>').insertBefore('#content');
$('.saved').hide().fadeIn(500).delay(1000).fadeOut(500);
setTimeout(function(){
$('.saved').remove();
},2000);
}
function q_display_failure_message (){
// Display "Failure" message when ajax returns failure
$('#ajax_loader').remove();
// Insert Overlay background
q_insert_overlay('body');
$('<span class="failure">There has been problem. Some of data might not be saved.<span id="small_close">close</span><span id="big_close">Close</span></span>').insertBefore('#content');
$('.saved').hide().fadeIn(500);
// Scroll the view point
q_scroll_top(110,250);
}
So here are the last two functions. These are visual functions so you do not need to use them and they might not be the right solution in your case. I have specified semi transparent background css image for popup_cover which brings the black overlay on top of which the Ajax messages are positioned.
function q_insert_overlay($body){
// Inserts the overlay into the page
$window_height = $($body).height();
$('<div id="popup_cover"></di>').insertBefore($('#page'));
$('#popup_cover').css({
'height': $window_height
});
}
function q_scroll_top ($top, $time){
$('html, body').animate({
scrollTop: $top
}, $time);
}
And this is it. This brings modern web application into the life where pages do not have to be reloaded and consume users time so user can focus more on the core of her/his work.