Attention! This post is older than three years, it may or may not be relevant anymore.

TL;DR

Different methods to post data within a form to any URL without refreshing the page using jQuery $.ajax function. It provides an example of a form with different input fields and shares some common and easy ways to post form data using AJAX, including creating an object of the FormData Class, serialize(), and manually appending data to the FormData object. It also explains how to attach files to the FormData object and post them using AJAX.

jQuery $.ajax function is quite a nifty way to post data within a form to any URL without refreshing the page and so is used extensively wherever jQuery is used. A typical jQuery $.ajax function looks like:

More information about the $.ajax function and it’s options can be found on the jQuery API Documentation page at http://api.jquery.com/jquery.ajax/

Now, there are several ways by which one can post all the data in a form using ajax. I’m going to discuss some of the most common and most easy ways per me, in no particular order.

First, let’s have a simple form as an example:

This will create a form like below:

 

Name:
Email:
Phone:
Vehicle:
I have a bike
I have a car
Select Car:

Select your favorite color:
Birthday (date and time):

Password:
Gender:
Male
Female
Other



Submit

 

I have taken all ‘types’ of input fields that are used commonly. You’ll notice that I have taken three type=’file’ fields, that’s because if we only have to attach just one file we can also do it in a different way but the method I use for attaching multiple files can be used for single files as well.

Number 1 (My Favorite)

The first method I’m going to discuss is my favorite because of its simplicity and compactness. In this method, we create an object of the FormData Class and post the data in one go.

In this method, all the types of input except the type file are posted and received in the $_POST superglobal variable. Therefore to attach the files we first get the properties of all the files from the file fields. As these properties contain a lot of data that we don’t need, we iterate the data using a for… in… loop and save the required files info in a new array. We then append that array to the newly created FormData object. This FormData object is then posted via the $.ajax function within the ‘data’ settings object. Note that in this method the ‘name’ attribute of the input field will be the ‘key’ for the $_POST array, for example when the below field is posted:

you can get it’s value on the server side using $_POST[‘phone’].

As you can see, this method is fairly simple, self-explanatory, and quite compact.

Number 2 – Get all input types

Another useful method is also quite popular, for reasons unknown to me. But still, it needs to be discussed. In this method, we wrap all our form fields inside a div for the sake of simplicity and simply assign all the ‘inputs’ values within that div to a variable and post that in the ‘data’. Checking out below will make more sense.

first, we wrap our form fields inside a div tag like:

As you can see, all the fields are inside the div with the id “testform-div”. Now for our ajax part:

Apart from the obvious ajax ‘data:’ field value, also notice the value of the ‘processData’ ajax field, it should be either set to true or should be omitted altogether as its default value is true. The processData option when true means that the ‘data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type “application/x-www-form-urlencoded”.’, as per the jquery ajax documentation, which means that the parameters or the input name and values would be converted into a string and sent along in the URL. This can mean security flaws in some cases. Other than this, as far as I know, there isn’t a way to post or upload files using this method. So, I don’t know why this method is popular but maybe someone can shed a light on it.

Number 3 – The traditional way

The third method is the traditional way. You take all the values of fields and create an object and then pass that object on to the ajax ‘data:’ option. This method might be lengthy but it’s the most reliable and also it gives you the ability to change the name or keys of the $_POST array variable to your own choice. On the downside, this method also doesn’t allow you to post or upload files and also has to ‘processData’. So, now for this method, our ajax code looks like this:

Notice the keys of the ‘formOject’ have been changed as per my liking. These keys would be received as the keys in the $_POST array in the PHP code. Also, notice that in this method too we have to either omit the processData and the contentType options or set them as ‘true’ for this to work.

As you can see, the first method is the best among the discussed as it’s more secure and also allows to upload files as well. I’m sure there are numerous other ways to upload files and post data using ajax and I would love to hear all of them out. Feel free to comment on anything that you might wanna discuss, share, or if just wanna say hi 🙂

To know more information check our web development services & contact us today!

That’s all.

Happy Coding 🙂

Currency
USD United States (US) dollar