JavaScript无法在ajax响应文件中运行,即chat.php

时间:2017-08-31 03:55:33

标签: javascript php ajax

我无法在chat.php中添加任何有效的JavaScript功能。基本上我使用AJAX和JavaScript创建了一个即时私人消息系统,但是糟糕的是chat.php不断地显示新消息,这是一件好事,但是阻止了我在chat.php中执行的任何JS。

chat.php适用于php html css等..但不是JS。请记住,我删除了想要放在chat.php中的JS。

下面发布的代码可以像设计一样显示即时消息,但是如果我添加任何JS功能,例如像document.write这样简单的东西它似乎不起作用。

chat.php显示它一秒钟,或者它只是没有显示我添加的任何JS正在执行。那可能是什么问题呢?我只想在chat.php中添加工作JS功能。 JS工作在index.php区域,不是针对chat.php。但是定位到chat.php会产生相同的结果。

的index.php

<?php
include("0/instructions/php/session.php");
$session = $_POST['set_session'];
$session = $_SESSION['set_session'];
$messenger_id = $user_id;
$partner_id= $_POST['partner_id'];


?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<meta charset="UTF-8">
<title>Chat System in PHP</title>
<link rel="stylesheet" href="style.css" media="all"/>
<script>
function ajax() {
var req = new XMLHttpRequest();
req.onreadystatechange = function(){
if(req.readyState == 4 && req.status == 200) {

document.getElementById('chat').innerHTML = req.responseText;

}
}
req.open('GET','chat.php',true);
req.send();

}
setInterval(function(){ajax()},1000);
</script>
</head>
<body onload="ajax();">
<div class="main_container">
<div id="container">
<div id="chat_box">
<div id="chat"></div>
</div>
</div>
<form method="POST" action="">
<div style="display: none;">
<input type="text" name="conversation_id" placeholder="conversation_id"  value="<?php echo $session; ?>"/>
<input type="text" name="member_name" placeholder="member_name" value="<?php echo $user_first_name;?> <?php echo $user_last_name;?>"/>
</div>
<textarea name="message" placeholder="enter message"></textarea>
<input type="submit" name="submit" value="Send it"/>
</form>
<?php
if(isset($_POST['submit'])){
$conversation_id = $_POST['conversation_id'];
$member_name= $_POST['member_name'];
$message= $_POST['message'];

$query = "INSERT INTO messages_x1 (conversation_id,member_name,message) values ('$conversation_id','$member_name','$message')";

$run = $connect->query($query);

if($run) {
echo "<div id='hide_audio'><embed loop='false' src='chat.mp3' hidden='true' autoplay='true'/></div>";
}

}
?>
</div>
</body>
</html>

chat.php

<?php
include("0/instructions/php/session.php");

$session = $_SESSION['set_session'];

$query= "SELECT * FROM messages_x1 WHERE conversation_id='$session' ORDER BY message_id DESC";
$run = $connect->query($query);

while($row = $run->fetch_array()) :
$messenger_id = $row['messenger_id'];
?>
<!DOCTYPE html>
<html>
<head>
<style>
.close_button {
    position: relative;
    float: right;
left: 8px;
    font-size: 25px;
    text-decoration: none;
  cursor:pointer;
bottom: 3px;
color: white;
background-color: transparent;
width: 30px;
height: 20px;
text-align: center;
}

.close_button p{
position: relative;
top: -15px;
}

.close_button:hover {
  color: red;
}
</style>
</head>
<body>
<div id="chat_data">
<span style="color:green;"><?php echo $row['member_name']; ?></span> :
<span style="color:brown;"> <?php echo  "<a class='close_button' href=\"delete.php?message_id=$row[message_id]\"onClick=\"return confirm('Are you sure you want to delete?')\">&times;</a>";?><br><?php echo $row['message']; ?></span>

</div>
<body>
</html>
<?php endwhile;?>

Screen Shot

1 个答案:

答案 0 :(得分:0)

您不应该使用完整的HTML页面来回应AJAX请求。只需返回要插入的代码段。所有CSS和JavaScript都应该驻留在index.php中,并根据需要进行调用。如果您确实需要从AJAX回复中传递JavaScript,则必须eval执行它。

现在通常只有JSON数据返回给AJAX调用。根据您的JavaScript操作的复杂程度,这可能会让您更容易处理。