gpt4 book ai didi

php - 未在索引页中显示消息

转载 作者:行者123 更新时间:2023-11-28 23:10:17 25 4
gpt4 key购买 nike

我正在尝试使用 php、mysql 和 ajax 创建小型聊天应用程序。使用 ajax 函数后,消息未显示在输出中。但数据正在传输到数据库。

索引页面代码

<?php

require('includes/core.inc.php');

if(isset($_POST['send'])){

if(send_msg($_POST['sender'], $_POST['message'])){
echo 'Message sent.';
}else{
echo 'message faild to sent.';
}

}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<!--Page title-->
<title>chat application</title>

<!--css stylesheet-->
<link type="text/css" rel="stylesheet" href="public/css/main.css"/>
</head>
<body>

<div id="input">
<form action="index.php" method="post">
<lable>Enter name:<input type="text" name="sender"/></lable>
<lable>Enter message:<input type="text" name="message"/></lable><br/>
<input type="submit" name="send" value="send message"/>
</form>
</div><!--input-->


<div id="messages">

</div> <!--Messages-->

<!--JAVASCRIPT-->

<script type="text/javascript" src="scripts/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="scripts/js/auto_chat.js"></script>


</body>
</html>

chat.php

<?php
require('../../includes/database/connect.db.php');
require('../../includes/functions/chat.func.php');

$messages = get_msg();
foreach($messages as $message){
echo '<strong>'.$message['sender'].' sent</strong><br/>';
echo $message['message'].'<br/><br/>';
}

?>

聊天功能

<?php

function get_msg(){

$query = "SELECT Sender,Message FROM chat";

$run = mysql_query($query);

$messages = array();


if($run === FALSE) {
die(mysql_error()); // TODO: better error handling
}


while($message = mysql_fetch_assoc($run)){

$messages[] = array('sender'=>$message['Sender'], 'message'=>$message['Message']);

}

return $messages;

}

function send_msg($sender, $message){
if(!empty($sender) && !empty($message)){
$sender = mysql_real_escape_string($sender);
$message = mysql_real_escape_string($message);

$query = "INSERT INTO chat VALUES (null , '{$sender}', '{$message}')";

if($run = mysql_query($query)){
return true;
}else{
return false;
}

}else{
return false;
}

}

?>

我添加的 ajax 部分是这个。我认为这是问题所在,但我找不到它。我以前没有使用 jquery 和 ajax。那么有什么前置条件吗?

$(document).ready(function() {
var interval = setInterval(function() {
$.ajax({
url: 'scripts/php/chat.php',
success: function(data){
$('#messages').html(data);
}
});
}, 1000);
});

我有数据库连接文件。但没有问题。如果需要我可以发布它。请帮助我

最佳答案

只需将下面的代码放在index.php 文件中

<?php
error_reporting(0);
$conn = mysql_connect('localhost','root','');
$db = mysql_select_db('test',$conn);
function send_msg($sender, $message){
if(!empty($sender) && !empty($message)){
$sender = mysql_real_escape_string($sender);
$message = mysql_real_escape_string($message);
$query = "INSERT INTO chat VALUES (null , '{$sender}', '{$message}')";
if($run = mysql_query($query)){
return true;
}else{
return false;
}
}else{
return false;
}
}
if(isset($_POST['send'])){
if(send_msg($_POST['sender'], $_POST['message'])){
echo 'Message sent.';
}else{
echo 'message faild to sent.';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<!--Page title-->
<title>chat application</title>
<!--css stylesheet-->
<link type="text/css" rel="stylesheet" href="public/css/main.css"/>
</head>
<body>
<div id="input">
<form action="" method="post">
<lable>Enter name:<input type="text" name="sender"/></lable>
<lable>Enter message:<input type="text" name="message"/></lable><br/>
<input type="submit" name="send" value="send message"/>
</form>
</div><!--input-->
<div id="messages">
</div> <!--Messages-->
</body>
</html>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script>
$(document).ready(function() {
var interval = setInterval(function() {
$.ajax({
url: 'test1.php',
success: function(data){
$('#messages').html(data);
}
});
}, 1000);
});
</script>

并为 ajax 调用创建一个文件 test1.php 并将代码放在下面

<?php
error_reporting(0);
$conn = mysql_connect('localhost','root','');
$db = mysql_select_db('test',$conn);
function get_msg(){
$query = "SELECT Sender,Message FROM chat";
$run = mysql_query($query);
$messages = array();

if($run === FALSE) {
die(mysql_error()); // TODO: better error handling
}

while($message = mysql_fetch_assoc($run)){
$messages[] = array('sender'=>$message['Sender'], 'message'=>$message['Message']);
}
return $messages;
}


$messages = get_msg();
foreach($messages as $message){
echo '<strong>'.$message['sender'].' sent</strong><br/>';
echo $message['message'].'<br/><br/>';
}

?>

以上对我有用。不需要任何其他 js 文件。

关于php - 未在索引页中显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46175206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com