jQuery Ajax Submit Form

jQuery Ajax Submit Form with Form-data Example


jQuery Ajax Submit Form

In this jQuery Ajax article, I share JQuery Ajax Submit form this will help you to make a submission form for Using JQuery Ajax.

In this tutorial, learn jquery ajax submit Form with the form data step by step. A simple jQuery Ajax example to show you how to submit a multipart form, using Javascript FormData and $.ajax().

If you will be using jQuery’s Ajax Form Submit, you can send the form data to the server without reloading the entire page. This will update portions of a web page – without reloading the entire page.

AJAX:- AJAX (asynchronous JavaScript and XML) is the art of exchanging data with a server and updating parts of a web page – without reloading the entire page.

Table Of Contents

  • Create HTML Form
  • jQuery Ajax Code

FAQs

  • How to add extra fields or data with Form data in jQuery ajax?
  • ajax FormData: Illegal invocation
  • How to send multipart/FormData or files with jQuery.ajax?

Create HTML Form

In this step, we will create an HTML form for multiple file uploads or FormData and an extra field.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html>
<html>
<title>jQuery Ajax Form Submit with FormData Example</title>
<body>
<h1>jQuery Ajax Form Submit with FormData Example</h1>
<form method="POST" enctype="multipart/form-data" id="myform">
  <input type="text" name="title"/><br/><br/>
  <input type="file" name="files"/><br/><br/>
  <input type="submit" value="Submit" id="btnSubmit"/>
</form>
<h1>jQuery Ajax Post Form Result</h1>
<span id="output"></span>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
</body>
</html>

jQuery Ajax Code

In this step, we will write jquery ajax code for sending a form data to the server.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
$(document).ready(function () {
  $("#btnSubmit").click(function (event) {
  //stop submit the form, we will post it manually.
  event.preventDefault();
  // Get form
  var form = $('#fileUploadForm')[0];
  // Create an FormData object
  var data = new FormData(form);
  // If you want to add an extra field for the FormData
  data.append("CustomField", "This is some extra data, testing");
  // disabled the submit button
  $("#btnSubmit").prop("disabled", true);
  $.ajax({
  type: "POST",
  enctype: 'multipart/form-data',
  url: "/upload.php",
  data: data,
  processData: false,
  contentType: false,
  cache: false,
  timeout: 800000,
  success: function (data) {
  $("#output").text(data);
  console.log("SUCCESS : ", data);
  $("#btnSubmit").prop("disabled", false);
  },
  error: function (e) {
  $("#output").text(e.responseText);
  console.log("ERROR : ", e);
  $("#btnSubmit").prop("disabled", false);
  }
  });
  });
});

FAQs – jQuery Ajax Form Submit

1. How to add extra fields with Form data in jQuery ajax?

The append() method of the FormData interface appends a new value onto an existing key inside a FormData object, or adds the key if it does not already exist.

1
2
3
4
5
// Create an FormData object
  var data = new FormData(form);
// If you want to add an extra field for the FormData
  data.append("CustomField", "This is some extra data, testing");

2. ajax FormData: Illegal invocation

jQuery tries to transform your FormData object to a string, add this to your $.ajax call:

1
2
processData: false,
contentType: false

3. How to send multipart/FormData or files with jQuery.ajax?

In this step you will learn how to send multiple files using jQuery ajax. Let’s see the below code Snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var data = new FormData();
jQuery.each(jQuery('#file')[0].files, function(i, file) {
  data.append('file-'+i, file);
});
$.ajax({
  url: 'upload.php',
  data: data,
  cache: false,
  contentType: false,
  processData: false,
  method: 'POST',
  success: function(data){
  console.log('success');
  }
});

Conclusion

In this jquery ajax form tutorial – you have learned how to send or submit the form data or multipart form using the jquery ajax on the server. Also, you have known the related queries of jquery ajax form.


 

Leave a Reply

Your email address will not be published. Required fields are marked *