<?
define("HOSTNAME", " ");
define("DBUSERNAME", " ");
define("DBPASSWORD", " ");
define("DATABASENAME", " ");
if (isset($_POST['ajax_request'])):
switch ($_POST['ajax_request_type']):
case "post":
echo postData();
exit();
break;
case "update":
if (($return = handle_ajax_request()) !== false):
if (empty($return)):
exit();
else:
echo "lastsuccessful=".time()."; var txt = '".ajax_escape($return)."';";
exit();
endif;
else:
exit();
endif;
break;
endswitch;
endif;
function ajax_escape($val) {
//adapted from sajax
$in = array("\\", "\r","\n","'",'"');
$out = array("\\\\", "\\r", "\\n", "\\'", '\\"');
return str_replace($in, $out, $val);
}
function dbconnect(){
mysql_connect(HOSTNAME,DBUSERNAME,DBPASSWORD);
mysql_select_db(DATABASENAME);
}
function postData(){
dbconnect();
if (empty($_POST['post_Name']) || empty($_POST['post_Text'])):
exit();
endif;
$query = "
Insert into posts
set
post_Timestamp = '".time()."',
post_Name = '".mysql_real_escape_string(trim($_POST['post_Name']))."',
post_Text = '".mysql_real_escape_string(nl2br(trim($_POST['post_Text'])))."'";
$result = mysql_query($query);
if (!$result) {
return $query;
} else {
return true;
}
}
function handle_ajax_request(){
//connect to db and select database
dbconnect();
$timestamp = !empty($_POST['lastsuccessful']) && $_POST['lastsuccessful'] !== "undefined"
? $_POST['lastsuccessful']
: 0; //defaults to last one hour
$query = "
Select
post_Name, post_Text
from
posts
where
post_Timestamp > '".$timestamp."'
order by
post_Timestamp desc";
//echo $query;
$chats = @mysql_query($query);
if (@mysql_num_rows($chats) > 0):
$return = "";
while ($chat = mysql_fetch_assoc($chats)):
$return .=
<<<STR
<div class="chat_row"><span class="name">{$chat['post_Name']}</span><span class="post">{$chat['post_Text']}</span></div>
STR;
endwhile;
return $return;
endif;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Chat Box</title>
<script type="text/javascript">
var cb, objInterval, lastsuccessful;
function init_object() {
//take from sajax
var A;
var msxmlhttp = new Array(
'Msxml2.XMLHTTP.5.0',
'Msxml2.XMLHTTP.4.0',
'Msxml2.XMLHTTP.3.0',
'Msxml2.XMLHTTP',
'Microsoft.XMLHTTP');
for (var i = 0; i < msxmlhttp.length; i++) {
try {
A = new ActiveXObject(msxmlhttp[i]);
} catch (e) {
A = null;
}
}
if(!A && typeof XMLHttpRequest != "undefined")
A = new XMLHttpRequest();
return A;
}
function sendUpdateRequest(){
//initialise variables
var strResult, pd;
//check if an xmlhttprequest object exists. if not create one
var objHTTP = init_object();
//open the object in post
objHTTP.open('POST',"chatbox.php",false);
//set the headers
objHTTP.setRequestHeader("Method", "POST " + "chatbox.php" + " HTTP/1.1");
objHTTP.setRequestHeader('Content-Type','application/x-[URL unfurl="true"]www-form-urlencoded');[/URL]
//push in the data that tells the server to give data
pd = "ajax_request=true&ajax_request_type=update&lastsuccessful="+lastsuccessful;
//send the request to the server
objHTTP.send(pd);
//grab the results
strResult = objHTTP.responseText;
//if the return value is more than 0 chars then push it on to the page
if (strResult.length > 0) {
insert_into_div (strResult);
}
}
function postMe(){
//build the post data
var pd = "post_Name="+document.getElementById("post_Name").value + "&post_Text="+ document.getElementById("post_Text").value+"&ajax_request=true&ajax_request_type=post";
var objHTTP = init_object();
//open the object in post
objHTTP.open('POST',"chatbox.php",false);
//set the headers
objHTTP.setRequestHeader("Method", "POST " + "chatbox.php" + " HTTP/1.1");
objHTTP.setRequestHeader('Content-Type','application/x-[URL unfurl="true"]www-form-urlencoded');[/URL]
//push in the data that tells the server to give data
//send the request to the server
objHTTP.send(pd);
//grab the results
strResult = objHTTP.responseText;
//if the return value is more than 0 chars then push it on to the page
if (strResult == true){
document.getElementById("post_Text").value = "";
}
}
function insert_into_div (str){
eval(str);
if (!cb){
cb = document.getElementById("chatbox");
}
cb.innerHTML = txt + cb.innerHTML;
}
function updateInterval(t){
clearInterval(objInterval);
if (t > 0)
objInterval = setInterval("sendUpdateRequest()", (t * 1000));
}
window.onload = function(){
objInterval = setInterval("sendUpdateRequest()", 5000);//5 second intervals
sendUpdateRequest();
}
</script>
<style>
body, input, button, textarea {
font-family:"Trebuchet MS", Arial;
font-size:12px;
}
textarea, input {
width: 70%;
}
textarea {
height: 80px;
}
#chatbox {
background-color:#CCFFCC;
width: 60%;
border:dotted red 1px;
}
#form {
width: 60%;
border:dotted red 1px;
background-color:#CCCCCC;
margin-bottom:10px;
padding-top: 10px;
}
.name{
clear:both;
float:left;
width: 20%;
}
.post {
float:right;
text-align:left;
width: 79%;
}
#updatediv {
margin-bottom: 10px;
}
</style>
</head>
<body>
<div id="updatediv">Update screen every:<input type="text" id="updateTime" name="updateTime" value="5" onchange="updateInterval(this.value);" style="width:2.5em; text-align:center;"/> seconds (0 to freeze)
</div>
<div id="form">
<span class="name">
Your Name:
</span>
<span class="post">
<input type="text" name="post_Name" id="post_Name" />
</span>
<span class="name">
Your post:
</span>
<span class="post">
<textarea name="post_Text" id="post_Text"></textarea>
</span>
<span class="name">
<button onclick="postMe();">Post</button>
</span>
<div style="clear:both;"> </div>
</div>
<div id="chatbox">
<div style="clear:both; visibility:hidden; line-height:0px;"> </div>
</div>
</body>
</html>