Solved

AJAX request returning text but display 'undefined'

Posted on 2010-08-20
5
488 Views
Last Modified: 2013-12-13
Hi fellow experts,

I suspect there is a silly explanation but it has been driving me nuts for two days now...

What i'm trying to do is save some data to a database through AJAX posting. Everything works as it's supposed to and the data is saved succesfully. At the end of the PHP page the script echoes if the operation has been succesfull. This also works correctly. When i check the received data from the server with the FireBug console in FireFox, i see that the word 'done' is returned which is what i want.

The function that saves the data is called from another function that saves the response to a variable like this:
result = saveForm();

Open in new window

So the result variable should have the word 'done' saved to it. But instead it has 'undefined'. Even stranger is the fact that when i place an alert in front of the return (like in the example) the alert gives the word 'done where the result variable has 'undefined'.

Does anyone have an idea? I hope someone can help me with this so i can move forward.
If you need more info or anything, let me know.

Thanks in advance, Michael
------------------------------
Javascript
------------------------------
// check if the form is saved
xmlHttp.onreadystatechange = function()
{
  if( xmlHttp.readyState == 4 )
  {
    if( xmlHttp.status == 200 )
    {
      alert(xmlHttp.responseText);
      // check if the page was executed succesfully
      return xmlHttp.responseText;
    }
  }
}
	
// open the page
xmlHttp.open( 'POST', url, false);
	
// set the request headers
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", parameters.length);
xmlHttp.setRequestHeader("Connection", "close");
	
// send the data
xmlHttp.send( parameters );

------------------------------
PHP
------------------------------
// execute the query and set the return message
if( $db->executeQuery($sql) )
  echo 'done';
else
  echo 'failed';

Open in new window

0
Comment
Question by:mverschoof
5 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 33484831
line 19 : xmlHttp.open( 'POST', url, true);
0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33484833
It sounds like you are trying to get a javascript variable to be assigned to a result from a php page... which I dont think it will do. Instead call the function saveForm(); which I am assuming is the ajax call... and then assign the variable like so

var result = document.getElementById('yourajaxdiv').innerHTML;

You might need to put this after the change state bit to make sure your php has finished.
 
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 33489216
try this


var result = "";
xmlHttp.onreadystatechange = function()
{
  if( xmlHttp.readyState == 4 )
  {
    if( xmlHttp.status == 200 )
    {
      result = xmlHttp.responseText;
      // myFunction(); // will use result
      // myFunction(result); // send result as parameter
    }
  }
}


then use result, or call a function after setting the result, or call a function with result as a parameter

Open in new window

0
 
LVL 5

Accepted Solution

by:
mverschoof earned 0 total points
ID: 33495492
I have figured it out myself. What i've done is save the variable outside of the readystatechange.
Because i used xmlHttp.open( 'POST', url, true) which is synchronous, i don't need the readystatechange to chekc if the server is done. With synchronous AJAX the script waits until the server is done before proceding with the rest of the script.

So what i have now is the return xmlHttp.responseText at the end of the script and i removed the onreadystatechanged function altogether.

Thanks for your efforts though
// open the page
xmlHttp.open( 'POST', url, false);
	
// set the request headers
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", parameters.length);
xmlHttp.setRequestHeader("Connection", "close");
	
// send the data
xmlHttp.send( parameters );

return xmlHttp.responseText;

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 33495526
:)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question