Page 1 of 2
Now that we know all about how to get a file, it is time to post some data back to the server. In this chapter we look at both get and post as ways of sending data and see how to send form data under Ajax control.
- The DOM
- CSS Selectors
- Advanced Filters
- Manipulating The DOM
- Promises & Deferred
- Promises, Deferred & WebWorkers
- Ajax the Basics - get
- Ajax the Basic - post
- Ajax - Advanced Ajax To The Server
- Ajax - Advanced Ajax To The Client
- Ajax - Advanced Ajax Transports And JSONP
- Ajax - Advanced Ajax The jsXHR Object
- Ajax - Advanced Ajax Character Coding And Encoding
- jQuery - Easy Plugins
- jQuery UI
- jQuery UI Custom Control - Widget Factory
- Getting Started With QUnit Testing
As explained in the previous chapter, both get and post retrieve data and both can send data to the server we tend to use get to retrieve data and post to send data.
Let's take a look at the simplest uses of post.
The jQuery post command is very easy to use but it can be difficult to get everything right on the client and on the server.
The basic command is works exactly like get:
which retrieves the file specified by the url and returns a jqXHR Promise object. You can specify a success callback as a parameter but as in the case of get it is easier to use the Promise object to define any callback.
this simply asks the server for the file myData.txt stored in the same directory as the page making the request.
You can use the done, fail and always Promise methods as described in the previous chapter.
This all works but it is unusual to simply use a post request to retrieve the contents of a file. A post usually sends data to the server in the body of the request and jQuery lets you specify this data a the second parameter. In principle you can send the data in any format you like as long as the server is able to deal with it. In practice jQuery provides automatic processing for a number of stanard formats and it is easier to stick with these.
For example the simplest format to use is to send key value pairs which jQuery will extract from an object you supply. So for example if you want to send a first name, second name and an id number you could use:
This sends the data to the server but of course the server just ignores it and sends the requested page back to the client.
To make use of the data there has to be a custom handler on the server to process the data. There are so many ways of doing this ranging from traditional CGI, JavaServlets, Node.js and so on.
For the sake of a concrete example we will use PHP to provide the server side processing.
The key idea is that the page that the post requests is best regarded as a program that processes the data and optionally returns a result to the client.
As long as you send data to a PHP page in key value pairs PHP will do you the favour of decoding them and will make them available as an associative array $_POST. This means that you have virtually no extra work to do on the server side to extract the data from a post.
You can also check that the request sent to the server was a post by testing the $_POST variable:
You also need to know that dots and spaces in the key will be converted to underscores e.g. a key like "I Programmer.info" is automatically converted to "I_Programmer_info".
You can handle the data sent by the previous example using a PHP file process.php something like;
which sends the first name back to the client within a web page.
PHP also automatically processes the value that you pass into either a string or an array according to its format. Notice that how and how much the data is preprocessed is a function of PHP not of the post request or Ajax transactions in general.
So for example if we post the data:
It could be processed in a PHP file something like:
Notice that the array is a string array even though the values are numbers. If you want to work with numeric values then it is up to you to convert the data.
Once again it is worth mentioning that it is the serverside system you use that provides the data processing facilities and hence these are going to vary according to what you use.
If you are using PHP then it is worth looking up the filter_input function which can perform validation and conversion as well as sanitization of input. For example if you want the id field to be returned as an integer you could use:
This is by far the best way to work with any input data - post, get, cookie or server.
So to sumarize:
- Post sends data in the body of the request and retrives data from the server but the file specified from the URL is expected to process the data sent.
- The data sent to the server can have any format but there are formats that are better supported than others inparticular a string of key value pairs.
Sending Data With Get
As mentioned in the previous chapter you can send data to the server with get almost as easily as with post but there are disadvantages.
When you send data to the server with get the data are encoded as the query string as part of the URL.
However from the point of view of using jQuery there is no real difference between sending data using post or get. For example to send some data to be processed by process.php you would use:
which apart from the change from post to get is identical to the earlier program.
At the server side you simply have to change the $_POST to $_GET:
This might well lull the beginner into a false sense of security because both the client and the server side systems are doing a lot of work to make things look this easy.
In particular jQuery does a lot of work for you and unpacks the key value pairs and URL encodes them. If you use the PHP instruction:
to send the URL back to the client for disply then you will discover that it reads something like:
The good news is that PHP automatically un-URLencodes the data for you and presents you with an array of key value pairs as in the case of post.
Notice that when you get all of the data is sent using the URL as a query string. Different browsers limit the lenght of a URL in different ways but traditionally a limit of around 2000 characters has been suggested. However why risk things not working when you can use a post instead.
var q= $.param(sendData));
If you want to URL encode a form use jQuery's serialize method.
It is also worth knowing about the serializeArray method which will convert form into an array of name, value pairs. This is often useful if you need to do a lot of processing before sending the data.
The bottom line is that there is little reason to use a get to send a lot of complext data - you are better of with a post.
- If you are sending data to the sever for processing use a post.
- If you are retrieving data from the server use a get.