簡體   English   中英

JavaScript在Ajax響應文件又稱為chat.php中不起作用

[英]JavaScript doesn't work in ajax response file aka chat.php

我無法在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;?>

屏幕截圖

您不應該用完整的html頁面來響應AJAX請求。 只需返回一個片段即可插入。 所有CSS和JavaScript都應駐留在index.php中,並根據需要調用。 如果確實需要通過AJAX答復傳遞JavaScript,則必須eval它才能執行。

通常,如今,只有JSON數據返回給AJAX調用。 根據您的JavaScript操作的復雜程度,這實際上可能會使您更容易處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM