Page 1 of 1

working with ajax

Posted: Tue Mar 25, 2008 2:17 am
by krraleigh
~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: :arrow: 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 :D ???

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()
 
// begin php scripting

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>';
 ?>
 
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: :arrow: Posting Code in the Forums to learn how to do it too.

Re: working with ajax

Posted: Tue Mar 25, 2008 3:40 am
by Christopher
You can get help here, but you posted a long block of code and did not put it in

Code: Select all

tags so it is not very readable. I would recommend editing your post to add

Code: Select all

tags and fix the indenting.

Re: working with ajax

Posted: Tue Mar 25, 2008 10:59 am
by pickle
I'd recommend using Firefox & installing the Firebug extension - that will REALLY help you find out what & where the problem is.

Re: working with ajax

Posted: Tue Mar 25, 2008 9:32 pm
by krraleigh
pickle wrote:I'd recommend using Firefox & installing the Firebug extension - that will REALLY help you find out what & where the problem is.
Is there a reason that my app would work with firefox and not IE 7 or safari?

thank you
Kevin

Re: working with ajax

Posted: Wed Mar 26, 2008 1:14 am
by krraleigh
I found the error, a simple typo
echo '<?xml version="1.0" encoding="URF-8" standalone="yes"?>';
should have been:

Code: Select all

 
echo '<?xml version="1.0" encoding="U[color=#008000]T[/color]F-8" standalone="yes"?>';
 
 

thank you
Kevin

Re: working with ajax

Posted: Wed Mar 26, 2008 9:52 am
by pickle
krraleigh wrote:Is there a reason that my app would work with firefox and not IE 7 or safari?
They're 3 different browsers with 3 different Javascript engines. Having your script work in 1 is no guarantee (though it's usually a fairly safe bet if you're using proper JS) it'll work in the others.

Glad to see you got it working.