Ajax reports "1" for IE

I am trying to create a functional page for parents who refuse to download anything other than IE to their machine (s). Anyway, its a list, and I just need some AJAX togglable buttons that were copied using the database.

I'm not sure what is going on, but the below turned out to be called and received perfectly both in Firefox and in Chrome. No matter what I do in IE (any version), I cannot get it to work. The onreadystatechange function always accepts and reports "1" instead of "off" or "on" as it should. This initially threw me away because I was just trying to send 1 and 0.

In any case, IE does not actually convey any values, so I assume that something is wrong with trying to go to the PHP page and get an answer. Sorry for being n00b, but what could cause this?

Unfortunately, the web page has some confidential information that I don’t want to see, so the touch code is below:

JAVASCRIPT FUNCTION:

function toggleResponded(i) {
    var respondedHttp;  
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        respondedHttp=new XMLHttpRequest();
    }   else    {// code for IE6, IE5
        respondedHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    alert("toggling responded for element: " + i);

    respondedHttp.onreadystatechange = function() {         
        if(respondedHttp.readyState==4 && respondedHttp.status==200) {
            var state = respondedHttp.responseText;
            alert("Recieved: " + state);
            if(state == "off") {
                document.getElementById("respond"+i).className = "inLineOff";
                document.getElementById("respond"+i).innerHTML = "No Response Yet";
                document.getElementById("attend"+i).style.display="none";
                document.getElementById("quantityCell"+i).style.display="none";
            } else if(state == "on") {
                document.getElementById("respond"+i).className = "inLineOn";
                document.getElementById("respond"+i).innerHTML = "Responded";
                document.getElementById("attend"+i).style.display="inline";
                if(document.getElementById("attend"+i).innerHTML == "Attending") {
                    document.getElementById("quantityCell"+i).style.display="table-cell";
                }
            } else {
                alert("error: " + state);
            }
        }
    }

    var name = peeps.people[i];

    alert("toggling for: " + name); 
    alert("toggling as: " + encodeURIComponent(name));

    var ops = "./quer.php?";    
    ops += "op=toggleResponded";
    ops += "&name=" + encodeURIComponent(name);

    alert("going to: " + ops);

    respondedHttp.open("GET", ops, true);
    respondedHttp.send();
}

PHP:

if(isset($_GET["op"])) {
    switch($_GET["op"]) {
        case "toggleResponded":
            $name = mysql_real_escape_string(rawurldecode($_GET["name"]));
            $database->executeStatement("SELECT * FROM table WHERE nameField='$name'");
            $info = $database->nextResult();
            $nval = 0;
            if($info["Responded"] == 0) {
                $nval = 1;
            }
            $database->executeStatement("UPDATE table SET field='$nval' WHERE nameField='$name'");
            if($nval == 1) {
                echo "on";
            } else {
                echo "off";
            }           
        break;
+3
source share
2 answers

IE caches ajax aggressively, and since you originally returned 1s and 0s, this is most likely only cached data. Add the current date to the URL you are requesting.

var d = new Date();
ops = ops + '&nocache=' + d.getTime();
+4
source

Use POST instead of GET and everything will be fine. IE caches GET, there is nothing mystical.

0
source

All Articles