How To Validate Form Fields And Post The Form Data To Server Using Jquery And Ajax?
Solution 1:
You do not need an external
click
handler because the plugin automatically captures theclick
(of atype="submit"
) and blocks the submit.You need an
input
orbutton
in yourform
that istype="submit"
, otherwise the plugin will not be triggered at all.You cannot have an external
.ajax
function while you haveform.submit
within thesubmitHandler
of your.validate()
method. This means the plugin is trying to submit the form while your click handler is trying to useajax
. They both cannot work at the same time. As per docs, anyajax
belongs inside thesubmitHandler
, and it's "the right place to submit a form via Ajax after it is validated".You don't need to check form validity because when you use the built in
submitHandler
, this is also done automatically.The jQuery4U code is nothing but complicated junk; everything can be simplified greatly. It serves no useful purpose other than to cause more confusion to those seeking guidance. It comes from a popular, yet poorly explained, online demo/tutorial by Sam Deering that is linked to/from many places.
$(document).ready(function(){ $("#register-form").validate({ rules: { userName: "required", email: { required: true, email: true }, userContactNumber: "required" }, messages: { userName: "Please enter your Name", userContactNumber: "Please enter your Mobile number", email: "Please enter a valid email address", }, submitHandler: function(form) { // get values from textboxs var uName = $('#userName').val(); var mailId = $('#addressemailId').val(); var mobNum = $('#userContactNumber').val(); $.ajax({ url:"http://192.168.1.11/services/bookService4Homes.php", type:"POST", dataType:"json", data:{type:"booking",Name:uName, Email:mailId, Mob_Num:mobNum }, //type: should be same in server code, otherwise code will not runContentType:"application/json", success: function(response){ //alert(JSON.stringify(response)); }, error: function(err){ //alert(JSON.stringify(err)); } }); returnfalse; // block regular submit } }); });
Post a Comment for "How To Validate Form Fields And Post The Form Data To Server Using Jquery And Ajax?"