AJAX更新db,但不替换<div> </div>

时间:2011-03-22 22:08:54

标签: php javascript mysql ajax

如此接近,但到目前为止。有一个表格显示故障单的状态(已提交,打开,关闭),单击故障单的ID时,会显示更多信息,并提供一个按钮以打开故障单或关闭故障单。该按钮有一个onClick事件,可以发送AJAX工作,只要更新票证的状态,该按钮就可以工作。然而,在更新时,显示状态的显示表也应该更新,但不会。

按钮:

            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>";
            }

表:

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.getElementById('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";
}
?>

1 个答案:

答案 0 :(得分:2)

在这一行

if (xmlhttp.readyState==4 && xmlhttp.status==200) {

删除

 && xmlhttp.status==200

它应该可以正常工作: - )