Log in

View Full Version : Resolved PHP chat



Demonicman
12-20-2014, 12:29 AM
I have modified a script that uses PHP, mysqli, and html to create a chat. Is there any way I can change it so it does not do meta refreshes to get the annoying blink from the loading?

If not, is there any other easy to work with chat script for another language I can use to access my mysql database?

Here is an example of what I have now:


<META HTTP-EQUIV=Refresh CONTENT="8;url=chatmsgs2.php">
<link rel=stylesheet>
<body background='bgic.JPG' border=1 bordercolor=white>


<?php

$csel = mysqli_query($db, "select * from chat order by id desc limit 50") or die("Could not get user stats");


while ($chat = mysqli_fetch_array($csel)) {



if($chat['viewer']!=""){

if($chat['viewer']==$_COOKIE['member_id']){

print "<p align=left>$chat[user] $chat[chat]<p align=left>";


}

}elseif($chat['chat'] == "") {

if($chat['sender']==$userstats3['Playername'] || $chat['reciever']==$userstats3['Playername']){

$blahblah=mysqli_fetch_array(mysqli_query($db, "select * from Users where playername='$chat[reciever]'"));

if($blahblah['ID']==""){

print "<p align=left><font style=\"color: gray\">No such user.</font><p align=left>";

}else{

if($chat['reciever']==$userstats3['Playername']){


print "<p align=left><font style=\"color: gray\"><a href='chat.php?Dude=$chat[sender]' target=_parent><font style=\"color: gray; text-decoration: underline;\">$chat[sender]</font></a> to $chat[reciever]$chat[message]</font><p align=left>";

}else{


print "<p align=left><font style=\"color: gray\">$chat[sender] to <a href='chat.php?Dude=$chat[reciever]' target=_parent><font style=\"color: gray; text-decoration: underline;\">$chat[reciever]</font></a>$chat[message]</font><p align=left>";

}

}

}

}else{

print "<p align=left>$chat[user] $chat[chat]<p align=left>";

}

}

?>

Demonicman
12-20-2014, 10:52 PM
Nobody responded so I figured it out myself. For future reference, here is a small bit of code I added.

chat.php = chatting form + div of chat called "chatlogs"
chatmsgs2.php = list of chat from db obtained using while() + fetch array

At the top of chat.php put this:


<script>

var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById('chatlogs').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open('chatmsgs2.php','chatmsgs2.php',true);
xmlhttp.send();

$(document).ready(function(e) {
$.ajaxSetup({cache:false});
setInterval(function() {$('#chatlogs').load('chatmsgs2.php');}, 2000);
});
</script>

Important: change 2000 to however much you want (2000 = 2 seconds)
the div must be named chatlogs

Found on youtube: https://www.youtube.com/watch?v=_XBeGcczFJo