Another way to send data from a Web page to the server is via POST method.
With Ajax, the request for sending data with POST uses the open() method of the XMLHttpRequest object, its syntax is:

open ("POST", URL, bool)
    - the "POST "is the method of transfer
    - the URL represents the address of the PHP file
    - bool is a Boolean value (true or false)
The difference from GET, in sending data via POST, consists of two things:
  1. After creating the request with "open()" and before calling the "send()" method, it is used another method of the XMLHttpRequest object, namely setRequestHeader(), using the syntax:
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
- This function indicate what type of content we are including, it sends a header to tell the server to recognize the sent data as if they were sent with POST (like data from Forms).
  2. The data we want to transmit to the PHP script, value pairs "index=value", are added as a string parameter in the send() method.

Processing the server response is the same as with the GET.
- To understand how to send data with AJAX using POST, apply and study the following example:
  1) Create on the server a PHP file, named "test_post.php", and add into it the following code:
// if data are received via POST, with index of 'test'
if (isset($_POST['test'])) {
    $str = $_POST['test'];             // get data
    echo "The string: '<i>".$str."</i>' contains ". strlen($str). ' characters and '. str_word_count($str, 0). ' words.';
- This is a relatively simple PHP script. If it receives data via POST, with index of 'test', takes its value in the $str variable and, with "echo" returns a string that contains the received text and the number of characters and words in that text.
  2) Create an HTML file on the server (for example, named "ajax_post.html") in the same directory where the file "test_post.php" is, and add the following code into it.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" lang="ro">
<meta http-equiv="content-type" content="text/html; charset=iso-8859-2" />
<title>Example Ajax POST</title>

<script type="text/javascript"><!--
// create the XMLHttpRequest object, according browser
function get_XmlHttp() {
  // create the variable that will contain the instance of the XMLHttpRequest object (initially with null value)
  var xmlHttp = null;

  if(window.XMLHttpRequest) {		// for Forefox, IE7+, Opera, Safari, ...
    xmlHttp = new XMLHttpRequest();
  else if(window.ActiveXObject) {	// for Internet Explorer 5 or 6
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

  return xmlHttp;

// sends data to a php file, via POST, and displays the received answer
function ajaxrequest(php_file, tagID) {
  var request =  get_XmlHttp();		// call the function for the XMLHttpRequest instance

  // create pairs index=value with data that must be sent to server
  var  the_data = 'test='+document.getElementById('txt2').innerHTML;"POST", php_file, true);			// set the request

  // adds  a header to tell the PHP script to recognize the data as is sent via POST
  request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  request.send(the_data);		// calls the send() method with datas as parameter

  // Check request status
  // If the response is received completely, will be transferred to the HTML tag with tagID
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      document.getElementById(tagID).innerHTML = request.responseText;

<h5 style="cursor:pointer;" onclick="ajaxrequest('test_post.php', 'context')"><u>Click</u></h5>
<div id="txt2">This string will be sent with Ajax to the server, via POST, and processed with PHP</div>
<div id="context">Here will be displayed the response from the php script.</div>

- The Ajax script is executed when the user click on the "Click" word. It calls the ajaxrequest() function with two parameters. The first parameter represents the name of the php file where the data will be sent, and the second parameter is the ID of the tag where the server response will be displayed.
- The ajaxrequest() function gets the text from the tag with id="txt2" and add it in the "the_data" variable, as the pair of the index "test".
- The open() contain the sending method (POST), the name of the php file and true to handle the request asynchronously ("POST", php_file, true); ).
- The setRequestHeader() method is added to the request, to indicate that we send data as a content of a Form (with POST)
- After the request is sent with data we want to transmit to PHP ( request.send(the_data); ), when the response is completely received (request.readyState==4), it displays it with the document.getElementById(tagID).innerHTML = request.responseText; instruction.
- When you open the "ajax_post.html" file from the server, will display the falowing result. Test it yourself.
This string will be sent with Ajax to the server, via POST, and processed with PHP
Here will be displayed the response from the php script.

The advantage of using POST is that you can send more data content than with the GET method (which is limited to a total of 1024 characters).

Daily Test with Code Example

Which attribute is used in <a> tag for the address of the link?
src href rel
<a href="" title=""></a>
Which CSS property sets the type of the text font?
font-family text-decoration font-size
h2 {
What instruction selects all the <div> tags with class="cls"?
querySelector("div.cls") getElementsByTagName("div") querySelectorAll("div.cls")
var elm_list = document.querySelectorAll("div.cls");
var nr_elms = elm_list.length;       // number of selected items
Indicate the function that can be used to get the sum of values in an array.
array_sum() array_diff() array_shift()
$arr = array(1, 2, 3, 4);
$arr_sum = array_sum($arr);
echo $arr_sum;       // 10
AJAX with POST and PHP

Last accessed pages

  1. PHP getElementById and getElementsByTagName (8740)
  2. Add data from form in text file in JSON format (5331)
  3. Add and Remove HTML elements and Content with jQuery (14616)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (27329)
  5. File Upload (1131)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (33841)
  2. PHP-MySQL free course, online tutorials PHP MySQL code (27329)
  3. Get Attribute (ID, Class, Name, Title, Src) with jQuery (25894)
  4. Read Excel file data in PHP - PhpExcelReader (25801)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (22639)