SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jun 2007
    Location
    Regina, SK, Canada
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with Ajax..

    Hi all! Could anyone tell me why this wont work...
    The function works when i remove the (ver) after "stateChanged" (inside showCat function). What I'm doing is I want to use this function more than once, so Im combining PHP to throw in a variable in showcat(str,ver). The ver is where the integer goes and auto-increments using a for loop that is being called through PHP
    This is my PHP... (which works fine)..

    <select name="blah" onchange="showCat(ver,$i)">
    <option>blah</option>
    </select>

    ... however the ajax wont work!

    Please help!

    <script type="text/javascript">
    var xmlHttp

    function showCat(str,ver)
    {
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
    alert ("Browser does not support HTTP Request")
    return
    }
    var url="getcat.php"
    url=url+"?id="+str
    xmlHttp.onreadystatechange=stateChanged(ver)
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
    }

    function stateChanged(ber)
    {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
    document.getElementById("txtHint"+ber).innerHTML=x mlHttp.responseText
    }
    }

    function GetXmlHttpObject()
    {
    var objXMLHttp=null
    if (window.XMLHttpRequest)
    {
    objXMLHttp=new XMLHttpRequest()
    }
    else if (window.ActiveXObject)
    {
    objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
    return objXMLHttp
    }
    </script>

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Welcome to SitePoint Forums, jboesch.

    To answer your question, I suggest you read this.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2007
    Location
    Regina, SK, Canada
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but it didn't really help me

    I want to use this function again and again and display the text in a diff element each time.. eg. txtHint1, txtHint2, txtHint3.. thats why im adding +ber to getElementByID("txtHint"+ber);

  4. #4
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That explained that this:
    Code:
    xmlHttp.onreadystatechange=stateChanged(ver)
    should be this:
    Code:
    xmlHttp.onreadystatechange=function(){stateChanged(ver)}
    That's not your only problem though. You don't seem to realize that the number outputted by the PHP will only be incremented when the page reloads.

    I am concerned that your code may not be following JavaScript Best Practices. I recommend you read up on them.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2007
    Location
    Regina, SK, Canada
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't post all of my PHP, but it works fine. The AJAX was my difficulty, I'm pretty new to it. I grabbed this code from a tutorial, but with that added suggestion you made, it all works!

    Thank you very much!


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
  •