Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

AJAX request returning text but display 'undefined'

Posted on 2010-08-20
5
Medium Priority
?
506 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 59

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

661 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