Solved

AJAX request returning text but display 'undefined'

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 dynamically set the form action using jQuery.

896 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

15 Experts available now in Live!

Get 1:1 Help Now