I am trying to submit a form using .ajax() jquery function
function submitForm()
{
$("#quiz").ajaxForm({
target: '#result',
type: 'post',
beforeSubmit: before_submit,
success: showResponse
}).submit();
}
the problem is form gets submitted in second attempt i.e. when i click twice.
$(document).ready(function(){
$("form#submit").submit(function() {
var fname = $('#fname').attr('value');
var lname = $('#lname').attr('value');
$.ajax({
type: "POST",
url: "ajax.php",
data: "fname="+ fname +"& lname="+ lname,
success: function(){
$('form#submit').hide(function(){$('div.success').fadeIn();});
}
});
return false;
});
});
Are you Initializing it in the $(document).ready(function())
? I hope not. First initialize it in the document ready minus the submit()
method and call submit()
in the submitForrm()
The things inside ajaxform()
must be initialized before calling them that is why most jquery plugins or widgets are initialized in the document ready section.
That is why when you call it for the first time it is initialized and on the second time it is actually executed.