Jump to content

ajax help


c_pattle

Recommended Posts

I'm just starting to use ajax and have the following code.  All it does is call the function updateForm when I change text in a text box and the end result should be an alert box saying "server is done".  However I keep getting the error message saying that "request is undefined".  However I made an alert box print out the contents of requestion and it came up with "[object XMLHttpRequest]" so I don't understand why. 

 

<script language="javascript" type="text/javascript">

function createRequest() {
var request;
  try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = false;
      }
    }
  }

  if (request)
    alert(request);

  if (!request)
    alert("Error initializing XMLHttpRequest!");
}
   function updateForm() {
createRequest();
     var company = document.getElementById("company_name").value;
     var url = "ajaxformupdate.php?company=" + escape(company);
     request.open("GET", url, true);
     request.onreadystatechange = updatePage;
     request.send(null);
   }

   function updatePage() {
     alert("Server is done!");
   }
</script>

Link to comment
https://forums.phpfreaks.com/topic/208654-ajax-help/
Share on other sites

Because you are not defining 'request' in the scope of the calling function. Here is the change

function updateForm() {
     var request = createRequest();
     var company = document.getElementById("company_name").value;
     var url = "ajaxformupdate.php?company=" + escape(company);
     request.onreadystatechange = function () { 
          if (req.readyState==4) {
               if (req.status==200) {
                    alert('Server is done!');
               } 
          }
     };
     request.open("GET", url, true);
     request.send(null);
   }

Link to comment
https://forums.phpfreaks.com/topic/208654-ajax-help/#findComment-1090147
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.