...

View Full Version : Ajax shoutbox



BinaryX
06-15-2010, 05:12 PM
Howdy!

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++){
try{
return new ActiveXObject(activexmodes[i])
}
catch(e){
//suppress error
}
}
}
else if (window.XMLHttpRequest) // if Mozilla, Safari etc
return new XMLHttpRequest()
else
return false
}


function updateShoutbox(){
var ajax_request= new ajaxRequest();
ajax_request.onreadystatechange=function(){
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")
}
else{
alert("An error has occured making the request")
}
}
}
}


shout.php:



<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="">
<table>
<tr>
<td>
<?php
$SHOUT_NOTICE = true;
$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">
<?php
} else {
?>
<input type="text" value="Nickname" size="25" maxlength="15" onclick="this.value='';" name="name">
<?php
}
?>
</td>
</tr>
<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>
</tr>
</table>

<div id="shout-frame">
<?php
if($SHOUT_NOTICE) {
echo '<div id="shout-notice">';
echo $SHOUT_NOTICE_TEXT;
echo '</div>';
}
?>
<div id="shout-body">

<?php

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

if(EMPTY($_POST)) {
?>
<script type="text/javascript">
setInterval('updateShoutbox()', 4000);
</script>
<?php
}
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>';

$i++;
}
}
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 ');
exit;

}
$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');
exit;
}
if(!$message_valid) {
$engine->displayError('Message cannot exceed 65 and must be atleast 3 characters.', 'Message Length');
exit;
}
$_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);
$engine->reloadPage("?p=shout");
}



?>

</div>
</div>
</form>


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.

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

BinaryX
06-16-2010, 10:21 AM
Bump .

katierosy
06-16-2010, 07: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