Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>AJAX</title>
  6. <script src="json.js"></script>
  7. </head>
  8. <body>
  9. <h1>Sök information om en användare</h1>
  10.  
  11. <label><b>Username</b></label>
  12. <input type="text" id="username" value="User">
  13.  
  14. <label><b>Password</b></label>
  15. <input type="text" id="pwd" value="password">
  16.  
  17. <button type="submit" id="loginButton">Login</button>
  18. <section id="userInfo"></section>
  19.  
  20.  
  21.  
  22.  
  23. </body>
  24. </html>
  25.  
  26.  
  27.  
  28. -----
  29. /** Deklarerar och instanserar vårt AJAX-objekt */
  30. var ajaxRequest=new XMLHttpRequest();
  31. var input;
  32.  
  33. function init(){
  34. input = document.getElementById('loginButton');
  35. input.addEventListener("click", sendRequest, false);
  36. }
  37.  
  38. /** Skickar begäran */
  39. function sendRequest(){
  40. ajaxRequest.onreadystatechange = getRequest;
  41.  
  42. var password = document.getElementById('pwd').value;
  43. var user = document.getElementById('username').value;
  44.  
  45. ajaxRequest.open("POST","login.php" , true);
  46. ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  47. ajaxRequest.send( "user="+user+"&password="+password);
  48. }
  49.  
  50. /** Tar emot begäran */
  51. function getRequest(){
  52. if (ajaxRequest.readyState==4 && ajaxRequest.status==200){
  53. var section = document.getElementById("userInfo")
  54.  
  55. // Rensar datalistan
  56. section.innerHTML=ajaxRequest.responseText;
  57. }
  58. }
  59.  
  60. window.addEventListener("load",init,false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement