I have a multiple Forms in a single page and i want to create an ajax call for each of them. I was thinking to do it by creating and object of the form's elements name and values and then pass them to the ajax call - by looping each form, so the result i'm looking for is something like this:
<form>
<input type="text" name="name_1">
<input type="password" name="password_1">
<input type="email" name="email_1">
<input type="submit" id="submit_1">
</form>
In Jquery
var formInputs = $(form);
for(var i=0; i < formInputs.length; i++){
var formInputItem = formInputs[i];
var formSubmitId = formInputItem - get the submit id // not sure how to do it
$(formSubmitId).click(function(e){
e.preventDefault();
// var name = $(formInputItem input[name=name]).val(); not show how to extract the element to achive this
var name = $("input[name=name]").val();
var password = $("input[name=password]").val();
var email = $("input[name=email]").val();
$.ajax({
type:'POST',
url:'/ajaxRequest',
data:{name:name, password:password, email:email},
success:function(data){
alert(data.success);
}
});
});
}
I know my explanation is a little vague but the idea is simple, I have multiple forms, and since each form has a different submit button's id, as well as different input names - the how can i pass the data to the Ajax call?