AJAX响应无法正确显示

时间:2013-08-14 12:13:30

标签: php ajax

<?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". $TicketInfo->ID ."'> </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".$TicketInfo->ID ."').innerHTML = ajax.responseText;
        }
        }
        ajax.send('delete=$ID');
    </script>
    ";
}
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".$TicketInfo->ID."').innerHTML = ajax.responseText;
        } 
        }
        ajax.send('close=$ID');
    </script>
    ";
}
}
if(isset($_GET['logout'])) {
session_destroy();
header('Location: login.php');
}
?>
<br />
</div>
</body>
</html>

响应文本显示在所有故障单旁边。我该如何解决这个问题?我只希望它显示在单击“删除”或“关闭”按钮的票证旁边。所有答案都非常感谢。提前致谢

delete.php

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

close.php

<?php
require('../configuration/config.php');
if(isset($_POST['close'])) {
$TID = $_POST['close'];
$checkExist = $con->query("SELECT * FROM tickets WHERE ID='$TID' AND open='true'");
$ch = $checkExist->num_rows;
if($ch >= 1) {
echo "Ticket Closed";
$con->query("UPDATE tickets SET open='false' WHERE ID='$TID'");
}
}

1 个答案:

答案 0 :(得分:0)

在您回复div的代码中,您应该将其写为(在刚刚提交的编辑问题中)

 <div id="ticket<?php echo $TicketInfo->ID; ?>">

或者如果在您的代码中,您应该

 echo "<div id='ticket".$TicketInfo->ID."'>"

不推荐使用 responseMsg id font 标记,为什么不使用 div ,可能会解决您的问题