jQuery chat / shout box
Posted: Fri Aug 05, 2011 9:47 pm
				
				Hey guys,
A one off from the white label game series.
http://www.youtube.com/user/GetTaptIn#g ... E8E70F2B1D
jQuery chat/shout box which can be used for any site. Also uses abit of jSON (I know jSON isnt needed for this, but it still teaches you abit about its use)
 (I know jSON isnt needed for this, but it still teaches you abit about its use)
Source code as requested
Chat.html
Chat.php
			A one off from the white label game series.
http://www.youtube.com/user/GetTaptIn#g ... E8E70F2B1D
jQuery chat/shout box which can be used for any site. Also uses abit of jSON
 (I know jSON isnt needed for this, but it still teaches you abit about its use)
 (I know jSON isnt needed for this, but it still teaches you abit about its use)Source code as requested

Chat.html
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function send_shout()
{
	var username = $('.js_username').val();
	var message = $('.js_message').val();
	
	$.post("chat.php", {mode : 'send-shout', user : username, shout : message});
	
	$('.js_message').val('');
}
function update_shout()
{
	var timestamp = $('.js_shouts .js_shout_hold:first-child input[type="hidden"]').val();
	var json = "";
	var formdata = "";
	
	$.ajax({
		
		type: "post",
		url: "chat.php",
		data: {mode : 'check-update'},
		success: function(msg){
			if(msg > timestamp)
			{
				
				var formdata = "mode=update-shout×tamp=" + timestamp;
				
				$.getJSON("chat.php", formdata, function(json){
					
					$.each(json, function(key, value)
					{
						var output = '<div class="js_shout_hold"><input type="hidden" value="' + value.timestamp + '" />' + value.user + ' says: ' + value.message + '</div>';
						$('.js_shouts').prepend(output);
					});
					
				});
				
			}
			
		}
		
	});
	
}
</script>
</head>
<body onload="setInterval('update_shout()',1000)">
<p>
	Username: <input type="text" name="username" class="js_username" />
</p>
<p>
	Message: <input type="text" name="message" class="js_message" size="80" maxlength="150" /> » <input onclick="send_shout();" type="submit" name="shout" class="js_shout" />
</p>
<div class="js_shouts" style="width: 600px; border:thin solid black; height: 150px; overflow-x: scroll">
	<div class="js_shout_hold"><input type="hidden" value="0" /></div>
</div>
</body>
</html>
Chat.php
Code: Select all
<?php
$connect = new mysqli('localhost', 'root', '', 'testing');
if($_POST['mode'] == "send-shout")
{
	$query = "INSERT INTO chat SET
				username = '{$_POST['user']}',
				message = '{$_POST['shout']}',
				timestamp = UNIX_TIMESTAMP(NOW())";
				
	$connect->query($query);
}
elseif($_POST['mode'] == "check-update")
{
	$query = "SELECT timestamp FROM chat ORDER BY timestamp DESC LIMIT 1";
	$sql = $connect->query($query);
	$row = $sql->fetch_object();
	
	echo $row->timestamp;
}
elseif($_GET['mode'] == "update-shout")
{
	$query = "SELECT * FROM chat WHERE timestamp > {$_GET['timestamp']}";
	
	$sql = $connect->query($query);
	
	while($row = $sql->fetch_object())
	{
		$data[] = array(
			"user"		=>	$row->username,
			"message"	=>	$row->message,
			"timestamp"	=>	$row->timestamp,
		);
	}
	
	echo json_encode($data);
	
}
$connect->close();
?> thanks torn for clearing that up, my head was going to explode.
 thanks torn for clearing that up, my head was going to explode.