Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Using Ajax with onClick issues

    I'm calling an Ajax function with an onClick command (sorry if some of this terminology is wrong) but it returns the previous querys results.

    For instance when you load the page and click the button for 'item 1' it shows nothing, click again for 'item 1' and it shows. If I then click the button for 'Item 2' it shows the results for 'item 1' but if I click again it the shows the results for 'item 2'. - Still with me...

    I'm calling a javascript function called show_comments with my onClick, please see below.

    The code is:
    Code:
    xmlHttp = GetXmlHttpObject();
    function show_comments(mystr)
    {
    	if (xmlHttp == null)
    		{
    			alert ("Yo browser is rubbish man!  Upgrade it");
    			return;
    		}
    	var url = "getnewscomments.php";
    	url+="?q="+mystr;
    	
    	xmlHttp.onreadystatechange=stateChanged(mystr);
    	xmlHttp.open("GET",url,true);	
    	xmlHttp.send(null);				
    } //end function showcomments
    
    function stateChanged(mystr,mystr1)
    {
    	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    		{
    			txtloc = "comments" + mystr;
    			document.getElementById(txtloc).innerHTML = xmlHttp.responseText;
    		}
    } //end function stateChanged
    
    function GetXmlHttpObject()
    {
    var xmlHttp=null;
    try
      {
      // Firefox, Opera 8.0+, Safari
      xmlHttp = new XMLHttpRequest();
      }
    catch (e)
      {
      // Internet Explorer
      try
        {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
      catch (e)
        {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
      }
    return xmlHttp;
    } //end function GetXmlHttpObject
    Any help qould be appreciated. Thanks.

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Try to change this part:
    Code:
    xmlHttp.onreadystatechange=stateChanged(mystr);
    into:
    Code:
    xmlHttp.onreadystatechange=function(){
    	stateChanged(mystr);
    }
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    howesy (01-14-2009)

  • #3
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Perfect, thanks for the help!


  •  

    Tags for this Thread

    Posting Permissions

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