AJAX not quite working right

Discussion in 'JavaScript' started by SHOwnsYou, Aug 17, 2009.

  1. #1
    I just started using ajax and I am having mixed results.

    Here is the code:

    On the page that calls the script:

    
    echo "<input type='text' id='txt1' value='$name' onmousemove=\"showHint(this.value)\" />";
    
    PHP:
    The script that gets called:
    
    $s = $_GET["s"];
    $s = mysql_real_escape_string($s);
    $ck = mysql_query("SELECT * FROM `user` WHERE `username` = '$s'");
    if (mysql_num_rows($ck) > 0)
    {
    mysql_query("DELETE FROM `user` WHERE `username` = '$s'");
    echo "<h1>User Deleted!</h1>";
    }
    else
    {
    echo "<h1>Not Deleted Yet!</h1>";
    }
    
    PHP:
    What happens, however, is that nothing gets echoed, but the delete query works perfectly. No "user deleted" or "not deleted yet" -- if I remove the delete query, nothing happens.
     
    SHOwnsYou, Aug 17, 2009 IP
  2. ct2k7

    ct2k7 Peon

    Messages:
    457
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Can you post more of the code? is $name ever defined?
     
    ct2k7, Aug 17, 2009 IP
  3. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    All the variables are defined and everything works including the sql query. What doesnt work is the echoing of text.

    What more of the code would you like to see?
     
    Last edited: Aug 17, 2009
    SHOwnsYou, Aug 17, 2009 IP
  4. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The ShowHint() function for starters, that way we can actually see if its a problem with the ajax or a problem with the script.
     
    kblessinggr, Aug 17, 2009 IP
  5. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It is copied almsot exactly from an example on w3schools. Hope this helps.

    
    var xmlhttp
    
    function showHint(str)
    {
    if (str.length==0)
      {
      document.getElementById("sid").innerHTML="";
      return;
      }
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
      {
      alert ("Your browser does not support XMLHTTP!");
      return;
      }
    var url="check.php";
    url=url+"?s="+str;
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }
    
    function stateChanged()
    {
    if (xmlhttp.readyState==4)
      {
      document.getElementById("sid").innerHTML=xmlhttp.responseText;
      }
    }
    
    function GetXmlHttpObject()
    {
    if (window.XMLHttpRequest)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
    }
    
    PHP:
     
    SHOwnsYou, Aug 17, 2009 IP
  6. ct2k7

    ct2k7 Peon

    Messages:
    457
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hmm, can you check that everything's correct using firebug in filezilla or something?
     
    ct2k7, Aug 17, 2009 IP
  7. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    All right, got it all fixed.
     
    SHOwnsYou, Aug 17, 2009 IP
  8. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Look into JQuery, Mootools or Prototype, since your function could simply be this (with jquery), and it would handle cross-browser compatbility far better than raw xmlhttp.

    
    function showHint(str)
    {
        if (str.length==0)
        {
            $('#sid').html('');
            return;
        }
        
        $('#sid').load('check.php?s='+str);
        //$('#sid').load('check.php', {s:str}); would be sent as a post request
    }
    
    Code (markup):
     
    kblessinggr, Aug 18, 2009 IP