Don't like ads? PRO users don't see any ads ;-)
Guest

registrere medlem

By: chaug90 on May 28th, 2012  |  syntax: JavaScript  |  size: 1.45 KB  |  hits: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function get_XmlHttp() {
  2.   // create the variable that will contain the instance of the XMLHttpRequest object (initially with null value)
  3.   var xmlHttp = null;
  4.  
  5.   if(window.XMLHttpRequest) {           // for Forefox, IE7+, Opera, Safari, ...
  6.     xmlHttp = new XMLHttpRequest();
  7.   }
  8.   else if(window.ActiveXObject) {       // for Internet Explorer 5 or 6
  9.     xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  10.   }
  11.  
  12.   return xmlHttp;
  13. }
  14.  
  15. // sends data to a php file, via POST, and displays the received answer
  16. function ajaxrequest(php_file, tagID) {
  17.   var request =  get_XmlHttp();         // calls the function for the XMLHttpRequest instance
  18.  
  19.  
  20. // gets data from form fields, using their ID
  21. var fornavn = document.getElementById('fornavn').value;
  22. var etternavn = document.getElementById('etternavn').value;
  23. var idrett = document.getElementById('idrett').value;
  24.  
  25. // create pairs index=value with data that must be sent to server
  26. var  the_data = 'fornavn='+fornavn+'&etternavn='+etternavn+'&idrett='+idrett;
  27. request.open("POST", php_file, true);
  28. request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  29. request.send(the_data);         // calls the send() method with datas as parameter
  30.  
  31. // Check request status
  32. // If the response is received completely, will be transferred to the HTML tag with tagID
  33. request.onreadystatechange = function() {
  34.   if (request.readyState == 4) {
  35.     document.getElementById(tagID).innerHTML = request.responseText;
  36.   }
  37. }
  38. }
  39. </script>