Topic
1 reply Latest Post - ‏2014-09-20T11:43:02Z by jitupkp
silverwatersask
silverwatersask
1 Post
ACCEPTED ANSWER

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(); 
} 

else 
{
// code for IE6, IE5 xmlhttp=

new ActiveXObject(
"Microsoft.XMLHTTP"); 
} xmlhttp.onreadystatechange=function() 
{   

if (xmlhttp.readyState==4 && xmlhttp.status==200) 
{ 
//document.getElementById("txtHint").innerHTML=xmlhttp.responseText;   allText = xmlhttp.responseText; lines = xmlhttp.responseText.split(
"<br>"); 
// Will separate each line into an array      
//document.write(xmlhttp.responseText); 
//document.write(allText); 
//document.write(lines[0]); 
//document.write(lines[1]); 
//document.write(lines[2]); 
//return allText; 
}   
} xmlhttp.open(
"GET",
"getadsbplanes.php?q="+str,

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>