Solved

AJAX request returning text but display 'undefined'

Posted on 2010-08-20
5
491 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

860 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