treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Complicated AJAX

  • Hi, I have sent from php and echo state ment - echo 'done' or echo 'notdone'
    this is, in ajax code - taken in as xmlhttp.responseText. This is my code in ajax.

    function stateChanged() {
    if (xmlhttp.readyState == 4 ) {
    responce = xmlhttp.responseText;
    if (responce == \"done\") {
    document.getElementById(\"box\").className = 'done';
    } else if (responce == \"notdone\"){
    document.getElementById(\"box\").className = 'notdone';
    }
    }
    }


    Its, not working - the class is not changing on the box id.
    If, i put this,

    function stateChanged() {
    if (xmlhttp.readyState == 4 ) {
    responce = xmlhttp.responseText;
    if (responce = \"done\") {
    document.getElementById(\"box\").className = 'done';
    }
    }
    }

    It works - notice how the first one, does not work at all. The second one has a single '=' next to responce = done. But this does not work when i add the other section - probably because im forcing responce to change.

    Any Help?

    Many THanks
  • Try this

    function stateChanged() {
    if (xmlhttp.readyState == 4 )
    {
    if(xmlhttp.status==200)
    {
    responce = xmlhttp.responseText;
    if (responce == \"done\") {
    document.getElementById(\"box\").className = 'done';
    } else if (responce == \"notdone\"){
    document.getElementById(\"box\").className = 'notdone';
    }
    }
    }
    }