one function for a range of forms
Hello,
I have the following code which just submits my login form via AJAX. It works fine. But I am wondering if I can adjust this to work for pretty much every form on my website, rather then copy paste this code over and over and only changing the values to match the form respectively.
-
$('#login').livequery('click', function() {
$("#contentmain").slideUp();
var username = $("input#username").val();
var password = $("input#password").val();
var dataString = 'username='+ $('input#username').attr("value") + '&password=' + $('input#password').attr("value");
$.ajax({
type: "POST", url: "lib/login.php", data: dataString,
async: true,
beforeSend: function(){$("#loading").show("fast");},
complete: function(){$("#loading").hide("slow");},
success: function(html){ //so, if data is retrieved, store it in html
changeTitle("Conflicting Forces - base");
$("#contentmain").slideDown("slow"); //animation
$("#contentmain").html(html); //show the html inside .content div
},
error: function(xhr, ajaxOptions, thrownError){
$("#contentmain").fadeIn("slow");
$("#contentmain").html('<p style="color: red; background-color: black; border: 1px solid red; padding: 2px;">ERROR: Page does not exist!</p>');
}
});
$.ajax({
method: "GET", url: 'rightpanel.php',
success: function(html){$("#right").html(html);}
});
$.ajax({
method: "GET", url: 'leftpanel.php',
success: function(html){$("#left").html(html);}
});
});
any help, ideas or suggestions are welcome and appreciated!
Cheers, Ace