我创建了一个由两个文件组成的私人消息系统。 index.php是基于发送私人消息而chat.php文件的设计是为了看到最初出现的新消息我用JavaScript用AJAX编写但是我决定用jQuery将其转换为AJAX所以我需要知道如何使用jQuery为整个文件调用chat.php添加一个setinterval,这样我就可以看到更新的消息,我可以用它来处理我最初创建的JavaScript版本。这是一个屏幕截图和文件。
的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>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#chat").load("chat.php");
});
</script>
<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"/>
</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?')\">×</a>";?><br><?php echo $row['message']; ?></span>
</div>
<body>
</html>
<?php endwhile;?>
答案 0 :(得分:0)
首先,我希望这只是为了学习目的。 这是一个典型的SQL注入漏洞。
只需将其用作常规js:
$(document).ready(function(){
setInterval(function(){
$.post({url:"...."});
}, 3000);
});
了解更多信息: jQuery.post