Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A simple AJAX project - My very first ajax project - Need help!!!

Status
Not open for further replies.

josel

Programmer
Oct 16, 2001
716
US
Hello!

I am reading off a book and came across the very first project. I typed all three programs (.htm, .js and .php) but I am not getting the expected results.

I decided to merge .htm and .js document to help trouble-shooting ... Here is the code I have

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>AJAX with PHP: Quickstart</title>
<script type="text/javascript">
// JavaScript Document
// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject();

// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject()
{
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// if running IE
	if(window.ActiveXObject)
	{
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp = false;
		}
	}
	// if running Mozilla or other browsers
	else
	{
		try
		{
			xmlHttp = new XMLHttpRequest();
		}
		catch (e)
		{
			xmlHttp = false;
		}
	}
	// return the created object or display an error message
	if (!xmlHttp)
	{   // <<< This was missing >>>
		alert("Error creating the XMLHttpRequest object.");
	 }  // <<< This was missing >>>
	 else
	 {     <<< This was missing >>>
		return xmlHttp;
	}   // <<< This was missing >>>
}	// function createXmlHttpRequestObject()

// make asynchronous HTTP request using the XMLHttpRequest object
function process()
{
	// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
		// retrieve the name typed by the user on the form
		name = encodeURIComponent(document.getElementById("myName").value);
		// execute the quickstart.php page from the server
		xmlHttp.open("GET", "quickstart.php?name=" + name, true);
		// define the method to handle server responses
		xmlHttp.onreadystatechange = handleServerResponse;
		// make the server request
		xmlHttp.send(null);
	}
	else
	{
		// if the connection is busy, try again after one second
		setTimeout('process()', 1000);
	}
} // function process()	

// executed automatically when a message is received from the server
function handleServerResponse()
{
	// move forward only if the transaction has completed
	if (xmlHttp.readyState == 4)
	{
		// status of 200 indicates the transaction completed successfully
		if (xmlHttp.status == 200)
		{
			// extract the XML retrieved from the server
			xmlResponse = xmlHttp.responseXML;
			// obtain the document element (the root element) of the XML structure
			xmlDocumentElement = xmlResponse.documentElement;
			// get the text message, which is in the first chile of
			// the document element
			helloMessage = xmlDocumentElement.firstChild.data;
			// update thecliendisplay using the data received from the server
			document.getElementById("divMessage").innerHTML = '<i>' + helloMessage + '</i>';
			// restart sequence
			setTimeout('process()', 1000);
		}  
		// a HTTP status different than 200 signals an error
		else
		{
			alert("There ws a problem accessing the server: " + xmlHttp.statusText);
		}			// close - if (xmlHttp.status == 200)
	}				// close - if (xmlHttp.readyState == 4)
}					// close - function handleServerResponse()
</script>
</head>
<body onload='process()'>
	TYPE YOUR NAME:
	<input type="text" id="myName" />
	<div id="divMessage" />
</body>
</html>

and here is the PHP script
Code:
<?PHP
// we'll generate XML output
header('Content-Type: text/xml');
// generate XML header
echo '<?xml version="1.0" encoding="UTF-8" standardlone="yes"?>';
// create the <response> element
echo '<response>';
// retrieve the user name
$name = $_GET['name'];
// generate output depending on the user name received from client
$userNames = array('CRISTIAN', 'BOGDAN', 'FILIP', 'MIHAI', 'YODA', 'JOSE');
if (in_array(strtoupper($name), $userNames))
{
	echo 'Hello, master ' . htmlentities($name) . '!';
} elseif (trim($name) == '') 
{
	echo 'Stranger, please tell me your name!';
} else 
{
	echo htmlentities($name) . ', I don\'t know you!';
}
// close the <response> element
echo '</response>';
?>

I am getting an error in the
Code:
function handleServerResponse()
{
....
....
helloMessage = xmlDocumentElement.firstChild.data;
....
....
}

I am not sure how reliable the error is as far as pointing to the actual line where the error occurs. I've been around long enough to know that the error could have started long before this point but still cannot find it.

What am I doing wrong? The book sample was missing a couple of open and close brackets "{}" in the if:else logic and I added them in ... I have not tried removing them as I believe that it will be worst without them ...

KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
On your XML(PHP) file, I changed this line:
Code:
echo '<?xml version="1.0" encoding="UTF-8" standardlone="yes"?>';
to this:
Code:
echo '<?xml version="1.0" encoding="UTF-8"?>';
and it worked for me in firefox.
 
also, double check your code, because in the code you posted, this line produces an error:
Code:
     {     <<< This was missing >>>

I'm assuming you meant to comment the <<< part, but just make sure that's commented in your code.
 
as far as the book example, it's my understanding that in JavaScript, along with other languages, if you leave off the { and } for an IF statement, then it will only execute the very next line, however I do not make a practice of this, i almost always use { and }. Example:
Code:
var do_it = true;
if(do_it) alert('boom');
is the same as
Code:
var do_it = true;
if(do_it){
   alert('boom');
}
Here's an example of how not to do it:
Code:
var do_it = true;
if(do_it)
   do_it = false;
   alert('boom');
is NOT the same as
Code:
var do_it = true;
if(do_it){
   do_it = false;
   alert('boom');
}
 
solepixel,

I spent a long time looking at the JavaScript. I even ran the PHP script by itself by passing the argument on the address bar and got what I thought was the proper results.

It goes to show that one must have a special gift to be a true integrated application developer.

As far as the commented line, I added those on my post because I made mention that they were omitted in the book and I had added them myself.

Thank you for the quick explanation on the if:else logic, I guess that the book has correct syntax since there is only one line per condition. Some things you just cannot learn even if reading books, which is what makes mediums such as this so valuable - Of course, no to mention members such as yourself.

Thank you so very much !!!


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
It does not work in IE, same error as originally reported. In all these years I had never thought of MS family of products as @#@**$@$@ - Now that I am playing around with the constant beating I get from IE (on top of PHP, JS, mySQL, Apache ... :) ... ) is just enough to make one wonder.

Regards,


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
It looks like ActiveX is my problem ... I found this article


and it mentions this as a problem with IE and AJAX.

That said, I do have it disabled in my W2003 server for security reasons :) which is the very server where I'm testing said code.

Thanks,


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top