SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    England
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    basic ajax script wont work.

    hello,

    i've been dabbling in ajax tonight and reading the ibm tutorials for it. Ive copied their code and modified it slightly for my own use but it doesnt seem to work.

    The code is pasted below. im just playing about at the moment. Currently the page this script is on has 2 form fields - compid and action - and these get sent off to tracker.php which writes them to a database. this is actually happening correctly, and updatePage is being called, but xmlHttp.readyState never equals 4. I used an alert to show its value when it reaches updatePage and it is 1 even though the action has happened (the values gone in to the dtaabase ok).

    I also tried alerting the contents of xmlHttp.responseText at this point to see if it contained anything but it is empty.


    Anyone got any suggestions? cheers


    Code:
    <script language="javascript">
    /* Create a new XMLHttpRequest object to talk to the Web server */
    var xmlHttp = false;
    
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
        xmlHttp = false;
      }
    }
    
    
    if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
      xmlHttp = new XMLHttpRequest();
    }
    
    
    function sendRequest() {
    
      var compid = document.getElementById("compid").value;
      var action = document.getElementById("action").value;
      
     
      if ((compid == null) || (compid == "")) return;
      if ((action == null) || (action == "")) return;
    
      // Build the URL to connect to
      var url = "tracker.php?compid=" + escape(compid) + "&action=" + escape(action);
    
      // Open a connection to the server
      xmlHttp.open("GET", url, true);
    
      // Setup a function for the server to run when it's done
      xmlHttp.onreadystatechange = updatePage();
    
      // Send the request
      xmlHttp.send(null);
     }
    
    
    
    
    function updatePage() {
    
      if (xmlHttp.readyState == 4) {
        var response = xmlHttp.responseText;
        document.getElementById("resultText").value = response;
      }
    }
    
    
    </script>

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    England
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well i've fixed the problem, the request now gets to state 4 and ive got everything working ok


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •