Solved

AJAX request returning text but display 'undefined'

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now