I am creating a script and I need to learn how to use the AJAX to get information from a page that is called get_info.php?num=001 so for example lets say the page returned like "abc" if you visited get_info.php?num=001. How would I use AJAX to get "abc" from that page.
[script] function trackIt(trk_num,trk_srv) { xmlhttp.open("GET","trackIt.php?trackingNumber=" + trk_num + "&trk_srv" + trk_srv,true); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4) { document.write(xmlhttp.responseText) } } xmlhttp.send(null) [/script] Above is the script I am trying to use, however it does not work.
<script type="text/javascript"> function trackIt(trk_num,trk_srv){ if(window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); } else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function(){ if(xmlhttp.readyState==4 && xmlhttp.status==200){ document.write(xmlhttp.responseText) } } xmlhttp.open("GET","trackIt.php?trackingNumber=" + trk_num + "&trk_srv" + trk_srv,true); xmlhttp.send(); } </script> Code (JavaScript):
Do not reinvent the wheel. Use jQuery instead. Except you want to learn native AJAX. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>jQuery ::: Simple AJAX</title> <script type="text/javascript" src="others/jquery.js"></script> <script type="text/javascript"> function try_ajax() { $.get( './ajax-target-1.php?name=something', function(data) { $('#id_result').html(data); } ); } </script> </head> <body> <input type="submit" value="Try AJAX" onclick="try_ajax()" /> <div id="id_result">-</div> </body> </html> HTML:
For some reason this keeps returning undefined. I have checked all my variables but it doesn't seem tow ork.
In your trackIt.php script are you echoing any text? You will need to echo something to the brwoser in order for the xmlhttp.responseText to fetch anything.
I'm not sure. Could you please take a look? http://007vg.com/track.it http://007vg.com/track.it/trackIt.php. I tried looking at various aspects of the script but nothing is working :\