Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.