ajax help plz

Discussion in 'JavaScript' started by stats, Jan 27, 2010.

  1. #1
    Here's my code

    var xmlhttp
    
    function addThis(str, add, sid)
    {
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
      {
      alert ("Your browser does not support XMLHTTP!");
      return;
      }
    var url="/myscript.php";
    url=url+"?id="+str;
    url=url+"&action="+add;
    url=url+"&sid="+sid;
    xmlhttp.onreadystatechange=myStateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }
    
    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;
    }
    
    function myStateChanged(tagname)
    {
    if (xmlhttp.readyState==4)
      {
       document.getElementById("comparebox").innerHTML=xmlhttp.responseText;
      document.getElementById(tagname).innerHTML=xmlhttp.responseText;
      }
    }
    Code (markup):
    This works just fine until i change the:
    xmlhttp.onreadystatechange=myStateChanged;
    to
    xmlhttp.onreadystatechange=myStateChanged(str);

    I need to pass the variable str to the function myStateChanged so that it will find and change the content of the tag in my page.. but as soon as i add the "(str)" after the function's name it simply stops working :(

    Whats wrong with this ?
     
    stats, Jan 27, 2010 IP
  2. unigogo

    unigogo Peon

    Messages:
    286
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try this one,
     
    unigogo, Jan 28, 2010 IP