phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


PHP - AJAX: Testing Server Connection

Tag: AJAX, XMLHttpRequest, server, connection, test   Category: PHP Application
post: 18 Mar 2008 read: 152


PHP AJAX Step By Step Tutorial - part 11: So far, we still don't review about server connection error handler. Now, we modify our code and add error handler about connection. Let's do it.

Open calc.html that we create at this post. Then modify process(funcCalc) become like this:


function process(funcCalc)
{


  myDiv = document.getElementById("myDivElement");
  myDiv.innerHTML = "";
  
  // will be continued if xmlHttp isn't void
  if(!xmlHttp) return;

  if (xmlHttp.readyState == 0 || xmlHttp.readyState == 4)
  {
    try
    {
	  var param1 = document.getElementById("param1").value;
	  var param2 = document.getElementById("param2").value;
	  
	  var params = "param1=" + param1 + "¶m2=" + param2 + "&func=" + funcCalc;
	  
      xmlHttp.open("Get","calc.php?" + params, true);
      xmlHttp.onreadystatechange = handleRequestStateChange;
      xmlHttp.send(null);
    }
    catch(e)
    {
      alert("Can't connect to server\n" + e.toString());
    }
  }
  else
  // don't try to make server request if the XMLHttpObject is busy
  {
    alert("Can't connect to server, please try again later.");
  }
}



| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
CAPTCHA - part 3 : "Are you human or ....?" (Build Your CAPTCHA)

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting