AJAX updating db, but not replacing <div>
So close, yet so far. There is a table that displays the status of trouble ticket's (submitted, open, closed), when the ID of a ticket is clicked, more info is displayed and a button is given to Open the ticket or Close it. The button has an onClick event that sends the AJAX to work, and the button works as far as updating the status of the ticket is concered. Howeever, upon update the in the display table that shows the Status should update as well, but does not.
Button:
if ($ticketarray['status'] == "0") {
// print option to open ticket
echo "<form>";
echo "<input type=\"button\" name=\"". $ticketarray['id'] ."\" value=\"Open Ticket\" onClick=\"statusChange(". $ticketarray['id'] .")\" />";
echo "</form>";
}
if ($ticketarray['status'] == "1") {
// print option to close ticket
echo "<form>";
echo "<input type=\"button\" name=\"". $ticketarray['id'] ."\" value=\"Close Ticket\" onClick=\"statusChange(". $ticketarray['id'] .")\" />";
echo "</form>";
}
Table:
echo "<td name=\"statusholder\" style=\"padding: 0px;margin: 0px;\" /><div style=\"font-color: ". $fontcolor .";font-weight: bold;background-color: ". $statuscolor .";text-align: center;width: 100%;height: 100%;visibility: visible;\" name=\"statusdiv\">". statusTranslator($tixarray['status']) ."</div></td>";
AJAX:
function statusChange(str) {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElemen开发者_开发问答tById('statusdiv').style.visibility = hidden;
document.getElementById('statusholder').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","statuschange.php?id="+str,true);
xmlhttp.send();
}
STATUSCHANGE.PHP:
<?
include("./header.php");
if (isset($_GET['id'])) {
// valid request, get current status
$currentstatus = mysql_query("SELECT status FROM `table` WHERE id='". mysql_real_escape_string($_GET['id']) ."'") or die("Cannot get current ticket status ". mysql_error());
$currentarray = mysql_fetch_assoc($currentstatus) or die("cannot make array ". mysql_error());
if ($currentarray['status'] == "0") {
// currently Submitted, make Open
mysql_query("UPDATE `table` SET status='1' WHERE id='". mysql_real_escape_string($_GET['id']) ."'") or die("cannot update status ". mysql_error());
// send reformatted status div
echo "<div style=\"font-color: #000;font-weight: bold;background-color: #FFFF00;text-align: center;width: 100%;height: 100%;\" name=\"statusdiv_updated\">Open</div>";
}
if ($currentarray['status'] == "1") {
// currently Submitted, make Open
mysql_query("UPDATE `table` SET status='2' WHERE id='". mysql_real_escape_string($_GET['id']) ."'") or die("cannot update status ". mysql_error());
// send reformatted status div
echo "<div style=\"font-color: #000;font-weight: bold;background-color: #33CC00;text-align: center;width: 100%;height: 100%;\" name=\"statusdiv_updated\">Completed (Closed)</div>";
}
} else {
echo "nothing to do here";
}
?>
On this line
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
remove the
&& xmlhttp.status==200
and it should work fine :-)
精彩评论