Javascript/PHP AJAX function not working properly.

Discussion in 'JavaScript' started by pasychosheep, Jun 16, 2008.

  1. #1
    Hey all, this forum was quite helpful with the last question I asked, I've got another problem now. I'm trying to write a basic ajax function, where a javascript function will call a php script every few seconds and check a value against a sql database, and when that sql database matches the query the php script just returns the word "Done", which causes the javascript function to exit a loop and then submit a form.

    I haven't gotten nearly that far yet, though, because I've run into a bit of a snag. I was just testing out the basic concepts of the idea, and it seems the javascript isn't able to parse the returning PHP text properly, or something. Here's the code:

    javascript:

    function changeit(){
    document.getElementById('submitter').innerHTML = "Please Wait...";
    http.open('get', 'boogie.php');

    http.onreadystatechange = checkHandler;

    http.send(null);


    };

    function checkHandler(){

    if(http.readyState == 4){
    var response = http.responseText;
    if(response == "Done"){
    document.getElementById('submitter').innerHTML = "Boogie is done.";
    }
    }
    }

    PHP:

    <?
    $bork=1;
    if($bork==1){
    ?>

    Done

    <?
    };
    ?>


    Now, as you can see, the script should automatically set the "submitter" div inner html to "Boogie is done." However, it just stays set to "Please wait..." in execution. If I include an else clause to the "response=="Done"" if statement, it executes the else clause, which makes me think that for some reason, even though response is returning the word "Done", the javascript isn't reading it as just a straight string that says "Done"

    So I know that was rather drawn out, but does anyone have any idea why this may be?
     
    pasychosheep, Jun 16, 2008 IP