I have one simple chat worked with jquery, PHP and MySQLi... All working perfect, but I need too add time of sender message like
or
ps. If I am on wrong place please tell me how and where I can write.
Here is all code what I have in files, if someone want to use
PHP file chat.php like 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>
In directory script/php I have files chat.php and 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';
}?>
In JS I have next:
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;
});
and downloaded jquery-1.11.3.min
now lets go in folder Chat where I have connection on database
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 function
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;
}
}?>
and last file core.inc.php
<?php
$feedback = array();
require('chat/baza/konekcija_db.php');
require('chat/funkcija/chat.func.php');
?>
Well I solved this problem with code
ps. similary like say Iwan1993 but on my way
first ADD Time column in database with type datetime
next changed SELECT
then changed while
next changed INSERT
and in chat.php added next