0
<?php
session_start();
if(!$_SESSION['Admin']) {
header('Location: login.php'); exit();
}
?>
<!DOCTYPE HTML>
<html>
<head>
    <title> ticketExpress | Admin </title>
    <link rel='stylesheet' href='../assets/css/style.css'> 
</head>
<body>
<div id='containerAdmin'>
<h1> <img class='logo' src='../assets/images/logo.png' width='200' height='43'> </h1> <a href='?logout' class='logout'> Logout </a>
<h3> Open Tickets </h3>
<hr />
<?php
require("../configuration/config.php");
$GetTickets = $con->query("SELECT * FROM tickets WHERE open='true'");
while($TicketInfo = $GetTickets->fetch_object()) {
$Subject = $TicketInfo->Subject;
echo "<div id='ticket'>".$Subject ."<a href='?delete=$TicketInfo->ID'><img style='float:right'src='../assets/images/delete.png' width='15px' height='15px'></a><a style='float:right; color:red; text-decoration:none; margin-right:10px;' href='?close=$TicketInfo->ID'> Close </a><font style='float:right; margin-right:10px;  color:green;' id='responseMsg'> </font></div>";
}
if(isset($_GET['delete'])) {
$ID = $_GET['delete'];
echo "
<script type='text/javascript'>
    var ajax = new XMLHttpRequest();
    ajax.open('POST','delete.php', true);
    ajax.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    ajax.onreadystatechange = function () {
        if(ajax.readyState == 4 && ajax.status == 200) {
            document.getElementById('responseMsg').innerHTML = ajax.responseText;
        }
        }
        ajax.send('delete=$ID');
    </script>
    ";
}
if(isset($_GET['logout'])) {
session_destroy();
header('Location: login.php');
}
if(isset($_GET['close'])) {
$ID = $_GET['close'];
echo "
<script type='text/javascript'>
    var ajax = new XMLHttpRequest();
    ajax.open('POST','close.php', true);
    ajax.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    ajax.onreadystatechange = function () {
        if(ajax.readyState == 4 && ajax.status == 200) {
            document.getElementById('responseMsg').innerHTML = ajax.responseText;
        } 
        }
        ajax.send('close=$ID');
    </script>
    ";
}
?>
<br />
</div>
</body>
</html>

我的问题是,每当我单击删除时,ajax 响应总是出现在页面上的第一张票旁边(最上面的一张)

例如,如果我单击工单 21 旁边的“关闭”,AJAX 响应“工单成功关闭”将始终出现在页面上的第一张工单旁边(例如工单 1)

这是close.php

<?php
require('../configuration/config.php');
if(isset($_POST['close'])) {
echo "Ticket Successfully Closed";
$TID = $_POST['close'];
$con->query("UPDATE tickets SET open='false' WHERE ID='$TID'");
}

并删除.php

<?php
require('../configuration/config.php');
if(isset($_POST['delete'])) {
echo "Ticket Deleted";
$TID = $_POST['delete'];
$con->query("DELETE FROM tickets WHERE ID='$TID'");
}

一如既往地感谢所有答案!提前致谢

4

1 回答 1

0

您有多个具有相同 ID 的 DOM 元素。检查你的while循环。

代码document.getElementById('responseMsg')将始终获取 DOM 中存在的第一个元素。您正在显示具有相同 DOM id 的多张工单。

您需要将一些唯一标识符附加到要显示服务器响应的元素。

看看这一行:

echo "<div id='ticket'>".$Subject ."<a href='?delete=$TicketInfo->ID'><img    style='float:right'src='../assets/images/delete.png' width='15px' height='15px'></a><a style='float:right; color:red; text-decoration:none; margin-right:10px;' href='?close=$TicketInfo->ID'> Close </a><font style='float:right; margin-right:10px;  color:green;' id='responseMsg'> </font></div>";

您可以更改id='responseMsg'id='responseMsg{$TicketInfo->ID}'然后将此票证 ID 作为 JavaScript 函数中的参数发送以定位正确的“responseMsg”元素。

例如

echo "<div id='ticket{$TicketInfo->ID}'>". $Subject ."<a href='#' onclick='deleteTicket({$TicketInfo->ID});'><img src="..."></a> <a style='float:right; color:red; text-decoration:none; margin-right:10px;' href='#' onclick='closeTicket({$TicketInfo->ID});'> Close </a><span style='float:right; margin-right:10px;  color:green;' id='responseMsg{$TicketInfo->ID}'> </span></div>";

<script type='text/javascript'>
function deleteTicket(ticketID) {
  var ajax = new XMLHttpRequest();
  ajax.open('POST','delete.php', true);
  ajax.setRequestHeader('Content-type','application/x-www-form-urlencoded');
  ajax.onreadystatechange = function () {
    if(ajax.readyState == 4 && ajax.status == 200) {
      document.getElementById('responseMsg'+ticketID).innerHTML = ajax.responseText;
    }
  }
  ajax.send('delete='+ticketID);
}
</script>
于 2013-08-14T11:21:25.100 回答