+ Reply to Thread
Results 1 to 5 of 5

Thread: AJAX help

  1. #1
    denzil is offline x10 Sophmore denzil is an unknown quantity at this point
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    134

    AJAX help

    Hey. I'm busy with an AJAX script for filtering out usernames when typing a username into a text box. What happens is that when you start typing in a username into the text box, it takes the characters in the box and matches it with what is in the database and then returns the results.

    But because Ajax isn't always the fastest, especially not on free servers, I want to add a loading image. I get it right to hide and show the image (with what I believe one would call jQuery). However, when I try and combine everything it does not work. I know where the problem is, but I don't know how to fix it.

    In the onload of the image, I call a JS function to hide the image. When someone enters something in the textbox, onkeyup calls an AJAX functions. First, I show the image and then the ajax should happen, and when it is done, the loading image should be hidden again. But what happens is the image is hidden immediately, even if the AJAX is not done loading.

    Does anyone know how I can check for when the Ajax is done loading, so I can wait before hiding the image?

    Thank you

    Relevant code:
    Code:
    function usernameSearch(str)
      {
        if (str.length==0)
        { 
          document.getElementById("nameSearch").innerHTML="";
          document.getElementById("nameSearch").style.border = "0px";
          return;
        }
        else
        {
          $("#load").show(); //loading image show
        }
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
          {
            document.getElementById("nameSearch").style.border = "1px solid #A5ACB2";
            document.getElementById("nameSearch").innerHTML=xmlhttp.responseText;
          }
        }
        xmlhttp.open("GET","getUsername.php?q="+str,true);
        xmlhttp.send();
        $("#load").hide(); //loading image hide (a wait is needed here)
      }

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: AJAX help

    Code:
    function usernameSearch(str)
      {
        if (str.length==0)
        { 
          document.getElementById("nameSearch").innerHTML="";
          document.getElementById("nameSearch").style.border = "0px";
          return;
        }
        else
        {
          $("#load").show(); //loading image show
        }
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
          if (xmlhttp.readyState==4 ){
              $("#load").hide(); //loading image hide
              if(xmlhttp.status==200)
              {
                  document.getElementById("nameSearch").style.border = "1px solid #A5ACB2";
                  document.getElementById("nameSearch").innerHTML=xmlhttp.responseText;
              } else {
                  ;// code, if any, to handle 404, 301, 500, etc server errors
              }
              }
        }
        xmlhttp.open("GET","getUsername.php?q="+str,true);
        xmlhttp.send();
    
      }
    Move the hide code inside the onreadystatechange handler when you get a response. You have to separate it from the 200 (success) code, otherwise 500/404/301/etc errors will leave the image.
    Last edited by descalzo; 04-16-2011 at 01:43 PM.
    Nothing is always absolutely so.

  3. #3
    denzil is offline x10 Sophmore denzil is an unknown quantity at this point
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    134

    Re: AJAX help

    Cool thanx! Works like a charm.

    I have another issue though. Perhaps not as important though. If I clear the textbox the Ajax reloads as if there is still a value in the box. For instance if I enter the letter b, it loads all names with b. Then I erase the b, it reloads all the user names with b. And then if I press backspace in the textbox again it clears it properly. Does this sound familiar to anyone?

    ---------- Post added at 07:47 PM ---------- Previous post was at 07:46 PM ----------



    I also checked it in IE 8, and it did not work so well. In chrome the loading image would stay there the up to the very second the Ajax retrieves the names. In IE, however, the image would just seemingly randomly disappear, even though the names have not been retrieved yet.
    Last edited by denzil; 04-16-2011 at 02:51 PM.

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: AJAX help

    Second problem, move the .hide() to after the

    if(xmlhttp.status==200) {
    } else {
    }

    but still inside the

    if (xmlhttp.readyState==4 ){

    }

    construct.

    As to the first problem, how are you calling the usernameSearch ? ie, is it on change or on keypress, etc?
    Nothing is always absolutely so.

  5. #5
    denzil is offline x10 Sophmore denzil is an unknown quantity at this point
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    134

    Re: AJAX help

    Thanks so much. I'd give you more rep if I could you have been very helpful. I will try it as soon as my website is back online. And I believe I was using onkeyup. Perhaps I should rather try on change. I'll try and figure it out first and then I'll post back, and let you know if I need more help.

+ Reply to Thread

Similar Threads

  1. ajax?
    By djcustom in forum Programming Help
    Replies: 4
    Last Post: 02-18-2009, 08:31 PM
  2. Ads on AJAX
    By yanks111890 in forum Free Hosting
    Replies: 3
    Last Post: 09-15-2008, 10:29 PM
  3. Ajax with Php
    By nyanko in forum Programming Help
    Replies: 4
    Last Post: 09-08-2008, 11:54 PM
  4. AJAX IM Help
    By thezone1 in forum Scripts & 3rd Party Apps
    Replies: 0
    Last Post: 04-21-2008, 03:56 AM
  5. Using AJAX to Spy On You
    By subvertman in forum Scripts & 3rd Party Apps
    Replies: 1
    Last Post: 08-15-2005, 08:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers