Top » AJAX » SEND DATA If you find this page useful
please make a secure donation
My Account  |  Cart Contents  |  Checkout   

Sending Data to the Server

Using AJAX, sometimes you need to send data to the server.
This bit of code will retrieve data from a server (like the Get Data example) but it will also send data to the server.

<script>
function ajaxNow(_data)
{
  var xmlHttp;
  try
  {
    /* Firefox, Opera 8.0+, Safari */
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
    /* newer IE */
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      /* older IE */
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser is old and does not have AJAX support!");
        return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
      /* this puts the value into an alert */
      alert("Value read is: "+xmlHttp.responseText);
    }
  }
  xmlHttp.open("GET","ajax_file.php?value1="+_data,true);
  xmlHttp.send(null);
}
</script>
Notice the "ajax_file.php?value="+_data above
When this function is called, a parameter that is passed to it will be sent to the server as a GET or URL-REWRITING method. The variable posted is value1 with the value of the parameter. This is exactly the same as:
<form method=GET action="ajax_file.php">
<input type=text name=value1><input type=submit>
</form>
The same except the whole page is not submitted...