Ajax- Submit Form Using Ajax Simple Example

Below is simple code for htmt and jQuey script.

HTML Code

1
2
3
4
5
<form name="ajaxform" id="ajaxform" action="ajax-form-submit.php" method="POST">
First Name: <input type="text" name="fname" value=""> <br>
Last Name: <input type="text" name="lname" value=""> <br>
Email : <input type="text" name="email" value=""> <br>
</form>

Script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
jQuery("#ajaxform").submit(function(e)
{
 var postData = jQuery(this).serializeArray();
 var formURL = jQuery(this).attr("action");
 jQuery.ajax(
 {
  url : formURL,
  type: "POST",
  data : postData,
  success:function(data, textStatus, jqXHR)
  {
 
  },
  error: function(jqXHR, textStatus, errorThrown)
  {
  }
 });
    e.preventDefault(); //STOP default action
});

Note: Please be sure, jQuery library has included for the page.

Enjoy the day :) :)

No comments:

Post a Comment