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
and here is the PHP script
I am getting an error in the
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
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