irwan

AJAX FUNDAMENTAL 1 Ajax Application Using the responseText

Jul 4th, 2011
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>My Ajax Application</title>
  4. <script Language=”JavaScript”>
  5. function getXMLHTTPRequest() {
  6. try {
  7. req = new XMLHttpRequest();
  8. } catch(err1) {
  9. try {
  10. req = new ActiveXObject("Msxml2.XMLHTTP");
  11. } catch (err2) {
  12. try {
  13. req = new ActiveXObject("Microsoft.XMLHTTP");
  14. } catch (err3) {
  15. req = false;
  16. }
  17. }
  18. }
  19. return req;
  20. }
  21. var http = getXMLHTTPRequest();
  22. function getServerText() {
  23. var myurl = 'textserver.php';
  24. myRand = parseInt(Math.random()*999999999999999);
  25. var modurl = myurl+"?rand="+myRand;
  26. http.open("GET", modurl, true);
  27. http.onreadystatechange = useHttpResponse;
  28. http.send(null);
  29. }
  30. function useHttpResponse() {
  31. if (http.readyState == 4) {
  32. if(http.status == 200) {
  33. var mytext = http.responseText;
  34. document.getElementById('myPageElement')
  35. ..innerHTML = mytext;
  36. }
  37. } else {
  38. document. getElementById('myPageElement')
  39. ..innerHTML = "";
  40. }
  41. }
  42. </script>
  43. </head>
  44. <body onLoad="getServerText()">
  45. Here is the text returned by the server:<br>
  46. <div id="myPageElement"></div>
  47. <?php
  48. $days = array('Monday','Tuesday','Wednesday',
  49. 'Thursday','Friday','Saturday','Sunday');
  50. echo "<table border='2'>";
  51. echo "<tr><th>Day Number</th><th>Day Name</th></tr>";
  52. for($i=0;$i<7;$i++)
  53. {
  54. echo "<tr><td>".$i."</td><td>".$days[$i]."</td></tr>";
  55. }
  56. echo "</table>";
  57. ?>
  58. </body>
  59. </html>
Advertisement
Add Comment
Please, Sign In to add comment