I'm trying to create a functional page for parents who refuse to download anything except IE on to their machine(s). Anyhow, its a list and I just wanted some AJAX togglable buttons that were backed up by a database.
I'm not sure what's up but the below gets called and received fine in both firefox & chrome. No matter what I do in IE (any version) I can't get it to work. The onreadystatechange function always receives and reports "1" instead of "off" or "on" as it should. This originally threw me because I was just trying to send 1's and 0's.
Anyhow, IE doesn't actually commit any of the values, so I imagine there's something wrong with the attempt to get to the PHP page and receive a response. Sorry for being a n00b, but what can cause this?
Unfortunately, the web page has some sensitive info I don't want people to see, so sensored code 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;
IE caches ajax aggressively, and since you were originally returning 1s and 0s it is probably just cached data. Add the current date to the URL you're requesting.
var d = new Date();
ops = ops + '&nocache=' + d.getTime();
Use POST instead of GET and everything will be fine. IE caches GETs, there's nothing mystical happening.