View Full Version : Ajax shoutbox

06-15-2010, 04:12 PM

Been trying to make an shoutbox, it works perfectly so far but it all goes bad when im trying to add ajax for letting it auto refresh.

ajax_shout.js :

function ajaxRequest(){
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
for (var i=0; i<activexmodes.length; i++){
return new ActiveXObject(activexmodes[i])
//suppress error
else if (window.XMLHttpRequest) // if Mozilla, Safari etc
return new XMLHttpRequest()
return false

function updateShoutbox(){
var ajax_request= new ajaxRequest();
if (ajax_request.readyState==4){
if (ajax_request.status==200){
ajax_request.open("GET", "./shout.php?a=d", true)
//ajax_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
alert("An error has occured making the request")


<script type="text/javascript" src="./themes/default/jquery-1.4.2.js"></script>
<script type="text/javascript" src="./themes/default/ajax_shout.js"></script>

<form method="post" action="">
$SHOUT_NOTICE_TEXT = "Welcome to my Shoutbox!";
if(!empty($_SESSION["SHOUT_NICK"])) {
<input type="text" value="<?php echo $_SESSION['SHOUT_NICK'] ?>" size="25" maxlength="15" onclick="this.value='';" name="name">
} else {
<input type="text" value="Nickname" size="25" maxlength="15" onclick="this.value='';" name="name">
<tr><td><textarea name="msg" rows="6" maxlength="65" onclick="this.value='';">Message</textarea></td>
</tr><tr><td><input type="submit" name="post" value="Shout!"></td>

<div id="shout-frame">
echo '<div id="shout-notice">';
echo '</div>';
<div id="shout-body">


global $cfg;
global $conn;
global $engine;

if(EMPTY($_POST)) {
<script type="text/javascript">
setInterval('updateShoutbox()', 4000);
if(isset($_GET['a'])) {
$query = $conn->query('SELECT * FROM shoutbox ORDER BY id DESC');
$rows = mysql_num_rows($query);
if($rows < 1) {
echo '<div id="shout-notice">There currently are no shouts.</div>';
$i = 0;
while($i < $rows) {
$shout_message = mysql_result($query, $i, 'message');
$shout_date = mysql_result($query, $i, 'date');
$shout_author = mysql_result($query, $i, 'author');

$shout_message = str_replace("", "<font color='orange'>", $shout_message);
$shout_message = str_replace("[color=red]", "<font color='red'>", $shout_message);
$shout_message = str_replace("", "<b>", $shout_message);
$shout_message = str_replace("", "</b>", $shout_message);
$shout_message = str_replace("", "</font>", $shout_message);
echo '&nbsp;Written by: '.$shout_author.' at '.$shout_date.'<div id="shout-message">'.$shout_message.'</div><br>';

if(isset($_POST['post'])) {
$micro = time() >= $_SESSION['LAST_POST'] + 30 ? true : false;
if(!$micro) {
$lp = time() - $_SESSION['LAST_POST'];
$engine->displayError('You can only post a message every 30 seconds. Last post: '.round($lp).' second(s) ago.', 'Anti-Spam ');

$name = mysql_escape_string($_POST['name']);
$message = mysql_escape_string($_POST['msg']);

$name_valid = strlen($name) <= 15 && strlen($name) > 2 ? true : false;
$message_valid = strlen($message) <= 65 && strlen($message) > 2 ? true : false;

if(!$name_valid) {
$engine->displayError('Nickname cannot exceed 15 and must be atleast 3 characters!', 'Nickname Length');
if(!$message_valid) {
$engine->displayError('Message cannot exceed 65 and must be atleast 3 characters.', 'Message Length');
$_SESSION['SHOUT_NICK'] = $name;
$date = date ('Y-m-d H:i:s');

$message = str_replace("<","&lt;", $message);
$message = str_replace(">","&gt;", $message);
$query = $conn->query("INSERT INTO shoutbox VALUES('$message', '$name' , '$date','')");
$_SESSION['LAST_POST'] = microtime(true);



Please help, could anyone edit my script and make it work with auto refreshing. i know that might be alot to ask but in this way i will learn it quicker.

06-15-2010, 10:22 PM
Anyone that can help?

06-16-2010, 09:21 AM
Bump .

06-16-2010, 06:31 PM
If you will go for jquery ajax.post() $.post() each from jquery.com
it may help you more.

Else you may fix the bug by yourself, by giving alert message for each line to find where it fails on the ajax. If you have firebug with firefox, it is well and good.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum