IC SunsetThe developerWorks Connections platform will be sunset on December 31, 2019. On January 1, 2020, this forum will no longer be available. More details available on our FAQ.
  • 1 reply
  • Latest Post - ‏2014-09-20T11:43:02Z by jitupkp
1 Post

Pinned topic New to AJAX....how to I get the reponse data back into the main javascript

‏2012-03-07T21:00:25Z |
new to ajax.

not that good at javascript.

I have the following code which uses ajax to get data from a mysql database with php
once I call the ajax function to get the data....I want to use the data in the main script but I have not been able to show the data with the document.write statement.

you will see where I have been testing that the data is being returned from the php script and yes it is...

I thought that the lines variable would contain the data I want.

Thanks for your help.....an example would be most welcome.

<html> <head> <script type=
"text/javascript">   function getflights(str) 
{ test = 
"test inside the function";   document.write(
"im in the function<br>");   
//if (str=="") 
//  { 
//  document.getElementById("txtHint").innerHTML=""; 
//  return; 
//  }  

if (window.XMLHttpRequest) 
// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=

new XMLHttpRequest(); 

// code for IE6, IE5 xmlhttp=

new ActiveXObject(
} xmlhttp.onreadystatechange=function() 

if (xmlhttp.readyState==4 && xmlhttp.status==200) 
//document.getElementById("txtHint").innerHTML=xmlhttp.responseText;   allText = xmlhttp.responseText; lines = xmlhttp.responseText.split(
// Will separate each line into an array      
//return allText; 
} xmlhttp.open(

true); xmlhttp.send();       
}   </script>   
//============================================================== <body> <script type=
"text/javascript"> var string1; var test; 
//var allText; 
//allText = "mmmm";   string1 = 
"ADSB Planes updated every 5 seconds"; test = 
"test outside function"; document.write(string1+
"<br>");   lines = 

new Array(20);     
//function start() 
//setInterval ( "showuser()", 5000 ); 
//}     document.write(
"call function <br>"); getflights();   document.write(
"show data = ");       document.write(test);   document.write(allText);                       </script>     </body> </head>           </body> </html>