jquery与时间聊天

时间:2015-06-10 07:10:58

标签: php jquery mysqli chat

我有一个简单的聊天工作与jquery,PHP和MySQLi ...所有工作完美,但我需要添加时间的发件人消息,如

enter image description here

enter image description here

PS。如果我在错误的地方,请告诉我如何以及在哪里写作。

以下是我在文件中的所有代码,如果有人想要使用

  

PHP文件 chat.php ,如index.php

<?php

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

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title></title>

    <link type="text/css" rel="stylesheet" href="public/css/main.css" />
</head>
<body>
<div id="input">
    <div id="feedback"></div>
    <form action="#" method="post" id="form_input">
        <labele>Ime:<br /><input type="text" name="sender" id="sender" size="25"/></labele><br /><br />
        <labele>Poruka:<br /><textarea id="message" cols="61" rows="3"/></textarea></labele><br />
        <input type="submit" name="send" id="send" value="Posalji poruku"/>
    </form>
</div>
<div id="messages">

</div>

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

script / php 目录中,我有 chat.php send.php

文件

chat.php

<?php
require('../../chat/baza/konekcija_db.php');
require('../../chat/funkcija/chat.func.php');

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

send.php

<?php
require('../../chat/baza/konekcija_db.php');
require('../../chat/funkcija/chat.func.php');

if(isset($_GET['sender'])&&!empty($_GET['sender'])) {
    $sender = $_GET['sender'];

    if (isset($_GET['message'])&&!empty($_GET['message'])){
        $message = $_GET['message'];

        if(send_msg($sender, $message)){
            echo 'Poruka je poslata';

        } else{
            echo 'Poruka nije poslata';
        }

    } else{
        echo 'Poruka nije unijeta';
    }

} else{
    echo 'Ime nije unijeto';
}?>
  

在JS中我有下一个:

auto_chat.js

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

});

send.js

$('#form_input').submit(function(){
var message = $('#message').val();
var sender  = $('#sender').val();

$.ajax({
    url: 'script/php/send.php',
    data: { sender: sender, message: message},
    success: function(data){
        $('#feedback').html(data);

            $('#feedback').fadeIn('slow', function() {
                $('#feedback').fadeOut(6000);
            });
        $('#message').val('');
    }
});

return false;

});

  

并下载了jquery-1.11.3.min

现在让我们进入文件夹Chat,我在数据库上有连接

konekcija_db.php     

$db_host = 'localhost';
$db_user =  'root';
$db_pass =  '';

$db_name =  'chat';

if($connection = mysql_connect($db_host, $db_user, $db_pass))
{
    $feedback[] = 'Konekcija na Bazu...<br />';

    if($database = mysql_select_db($db_name, $connection))
    {
        $feedback[] = 'Baza je selektovana... <br />';
    }else
    {
        $feedback[] = 'Baza nije pronadjena!<br />';
    }
}
else{
    $feedback[] = 'Nije moguca konekcija na MYSQL server!<br />';
}?>
  

聊天功能

chat.func.php

<?php

function get_msg(){

    $query = "SELECT `Sender`, `Message` FROM `chat`.`chat` ORDER BY `Msg_ID` DESC";

    $run = mysql_query($query);

    $messages = array();

    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`.`chat` VALUES (null, '{$sender}', '{$message}')";

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

和上一个文件 core.inc.php

<?php
$feedback = array();
require('chat/baza/konekcija_db.php');
require('chat/funkcija/chat.func.php');
?>

2 个答案:

答案 0 :(得分:0)

您可能希望将DateColumn添加到

`chat`.`chat`

表。然后将插入更改为

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

你的选择

$query = "SELECT `Sender`, DATE_FORMAT(`Date`, '%Y-%m-%d') AS dateOnly, DATE_FORMAT(`Date`, '%H:%i:%s') AS timeOnly, `Message` FROM `chat`.`chat` ORDER BY `Msg_ID` DESC";

在JS中,您只需要显示时间。

答案 1 :(得分:0)

我用代码解决了这个问题

PS。类似于Iwan1993,但在路上

数据库中的第一个 ADD时间列 datetime

接下来改变了SELECT

 $query = "SELECT `Sender`, `Message`, DATE_FORMAT(`time`,'%d.%m.%Y/%H:%i:%s') as 'time' FROM `chat`.`chat` ORDER BY `Msg_ID` DESC";

然后改变了

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

接下来改变了INSERT

$query = "INSERT INTO `chat`.chat(Msg_ID,Sender, Message) VALUES (null, '{$sender}', '{$message}')";

并在chat.php中添加了下一个

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