Advertisement
lessientelrunya

ajaxrequest1

Jul 25th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // in your HTML:
  2.     <form>
  3.         <select name="staff"> onchange="cs50Info(this.value);">
  4.             <option value ="">Select someone:</opntion>
  5.             <option value="blumberg">Hannah Blumberg</option>
  6.             <option value="bowden">Rob Bowden</option>
  7.         </select>
  8.     </form>
  9.  
  10. // in your JS file:
  11. function CS50Info(name)
  12. {
  13.     // deal with the situation where nothing is chosen
  14.     if (name == "")
  15.         return;
  16.  
  17.     // create a new AJAX object
  18.     var ajax = new XMLHttpRequest();
  19.  
  20.     // when the page is loaded, have a callback function pre-fill our div
  21.     ajax.onreadystatechange = function() {
  22.         if (ajax.readyState == 4 && ajax.status == 200)
  23.             $('#infodiv').html(ajax.responseText);
  24.         }
  25.     };
  26.  
  27.     // open the requested file and transmit the data
  28.     ajax.open("GET", name + '.html', true);
  29.     ajax.send();
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement