I have some jQuery codes, which is repeated again and again, i would like to reduce the code i am writing that is by converting it into functions. here is the codes i am using.
$('form#save-user button[name="save-user"]').click(function() {
var formData = 'option=saveuser&'+$('form#save-user').serialize();
$.ajax({
type: 'POST',
url: 'process.php',
data: formData,
success: function(msg){
if(msg === 'empty') {
alert('Required Values Missing');
} else if(msg === 'duplicateEmail'){
alert('Email already exist');
} else {
window.location = "index.php?users&option=edit&user_id="+msg+'&msg=success';
}
}
});
});
$('form#save-user button[name="save-user-close"]').click(function() {
var formData = 'option=saveuser&'+$('form#save-user').serialize();
$.ajax({
type: 'POST',
url: 'process.php',
data: formData,
success: function(msg){
if(msg === 'empty') {
alert('Required Values Missing');
} else if(msg === 'duplicateEmail'){
alert('Email already exist');
} else {
window.location = 'index.php?users';
}
}
});
});
$('form#save-user button[name="save-user-new"]').click(function() {
var formData = 'option=saveuser&'+$('form#save-user').serialize();
$.ajax({
type: 'POST',
url: 'process.php',
data: formData,
success: function(msg){
if(msg === 'empty') {
alert('Required Values Missing');
} else if(msg === 'duplicateEmail'){
alert('Email already exist');
} else {
window.location = 'index.php?users&option=create';
}
}
});
});
I would like to know few things,
a) With reference to above code, how do i convert it to function, as the code have very few changes like, selector name and url of window.location.
b) what do i call the code below, is it the function? function on the go? or dynamic function?
$('selector').event(function(){
//jQuery Code in wake of event being triggered.
});