working with ajax
Posted: Tue Mar 25, 2008 2:17 am
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Is it possible to get a little help with some AJAX?
If not can you point me to a good forum
???
I have some code that is throwing an error because it is looking for an
object,
but my javascript is not as good as I would like it to be.
I have an html page that calls the process() function onload
in this page is a text box with the id="myName"
the error occurs in the handleServerResponse() function
error = //get the text message, which is in the first child of the
document element
helloMessage = xmlDocumentElement.firstChild.data;
evidently helloMessage is supposed to receive an object but dosen't
I tried using alerts to pin point the exact problem but I don't know what
xmlDocumentElement.firstChild.data; is supposed to do??
the php is really straight forward and is called with this line of code
xmlHttp.open("GET", "quckstart.php?name=" + name, true);
I'll post this code under the javascript
I think the problem is that the php page is not returning an object but
how do I check this?
// begin php scripting
if you can't advise can you tell me who can advise?
newsgroups, forums etc...
insight always appreciated
thank you
Kevin
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Is it possible to get a little help with some AJAX?
If not can you point me to a good forum
I have some code that is throwing an error because it is looking for an
object,
but my javascript is not as good as I would like it to be.
I have an html page that calls the process() function onload
in this page is a text box with the id="myName"
the error occurs in the handleServerResponse() function
error = //get the text message, which is in the first child of the
document element
helloMessage = xmlDocumentElement.firstChild.data;
evidently helloMessage is supposed to receive an object but dosen't
I tried using alerts to pin point the exact problem but I don't know what
xmlDocumentElement.firstChild.data; is supposed to do??
the php is really straight forward and is called with this line of code
xmlHttp.open("GET", "quckstart.php?name=" + name, true);
I'll post this code under the javascript
I think the problem is that the php page is not returning an object but
how do I check this?
Code: Select all
//the process function & the handleServerResponse functions
function process(){
//proceed only if the xmlhttp object isn't busy
if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
//retrive 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", "quckstart.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);
}//end function process()
}
//executed automatically when a messageis received from the server
function handleServerResponse(){
//move forward only if the transactiion has completed
if(xmlHttp.readyState == 4){
//status of 200 indicates the transaction completed successfully
if(xmlHttp.status == 200){
//extract the xml retrived 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 child of the document
element
helloMessage = xmlDocumentElement.firstChild.data;
//update the client display using the data received from the server
document.getElementById("divMessage").innerHTML = '<i>' + helloMessage +
'</i>';
//restart sequence
setTimeOut('process()', 1000);
} else {//a http status different than 200 signals an error
alert("there was a problem accessing the server: " +
xmlHttp.StatusTest);
}
}
}//end function handleserverresponse()
Code: Select all
<?php
//we'll generate xml output
header('Content-Type: text/xml');
//generate xml header
echo '<?xml version="1.0" encoding="URF-8" standalone="yes"?>';
// create the <response element
echo '<response>';
//retrive the user name
$name = $_GET['name'];
//generate output depending on the user name received from client
$userNames = array('CRISTIAN', 'BOGDAN', 'FILIP', 'MIHAI', 'YODA');
if (in_array(strtoupper($name), $userNames))
echo 'Hello, master ' . htmlentities($name) . '!';
else if (trim($name) == '')
echo 'Stranger, please tell me your name!';
else
echo htmlentities($name) . ', I don\'t know you!';
//close the <response> element
echo '</response>';
?>
newsgroups, forums etc...
insight always appreciated
thank you
Kevin
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: