简体   繁体   English

AJAX更新数据库,但没有替换<div>

[英]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. 有一个表格显示故障单的状态(已提交,打开,关闭),单击故障单的ID时,会显示更多信息,并提供一个按钮以打开故障单或关闭故障单。 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. 该按钮有一个onClick事件,可以发送AJAX工作,只要更新票证的状态,该按钮就可以工作。 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: 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: 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 :-) 它应该工作正常:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM