Can somebody please help me with my code
Posted: Tue Jul 14, 2009 8:15 pm
I'm trying to create a very simple AJAX shoutbox (will make it a little more advanced and add smilies and a control panel to it later) and I'm having some difficulties.
I'm using the tutorial from this site here http://www.aspektas.com/blog/an-ajax-shoutbox And I put everything together. But I'm getting an error.
I know errors like this a self explanatory but I'm still a novice and I am not able to make a code like this up on my own, I can only understand how it works, modify it's values and settings and make it more advanced (Odd, yes I know
).
Anyway the code is here:
index.php (code imbedded into my index.php file that shows the input box and shout messages)
config.php
shouts.php
getshouts.php
shout.php
shouts.js
I can't see what I've done wrong. But I'm guessing it has something to do with the fcontrol(). On the tutorial site they mentioned it for the config.php file but they never showed the config.php file so I don't know. Also the page seems to always refresh itself when trying to send a message which shouldn't happen. They mentioned that in the tutorial that if javascript is disabled that it will automatically refresh the page with new results but javascript is not disabled for me.
I'm using the tutorial from this site here http://www.aspektas.com/blog/an-ajax-shoutbox And I put everything together. But I'm getting an error.
Code: Select all
Fatal error: Call to undefined function fcontrol() in /home/insight/public_html/modules/shoutbox/shout.php on line 3Anyway the code is here:
index.php (code imbedded into my index.php file that shows the input box and shout messages)
Code: Select all
<p id="shoutbox"><?php include("modules/shoutbox/getshouts.php"); ?></p>
<form action="" method="post" onsubmit="return push_shout()">
<input type="text" name="user" id="user" />
<input type="text" name="shout" id="shout" />
<input type="submit" value="Shout" />
</form>
<p id="console"></p>Code: Select all
<?php
include("include/constants.php");
$sql=mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME, $sql) or die(mysql_error());
?>Code: Select all
<?php
require_once("modules/shoutbox/config.php");
echo implode(mysql_fetch_assoc(mysql_query("SELECT COUNT(id) FROM shout")));
?>Code: Select all
<?php
require_once("modules/shoutbox/config.php");
$result = mysql_query("SELECT * FROM shout ORDER BY id DESC LIMIT 10");
$lines = array();
while ($shout = mysql_fetch_assoc($result)) {
$lines[] = "{$shout['username']} (" .
date("H:i:s", $shout['datetime']) .
") :: {$shout['shout']}<br />";
}
echo implode(array_reverse($lines));
?>Code: Select all
<?php
require_once("modules/shoutbox/config.php");
if (fcontrol("shout", 5)) {
$user = mysql_real_escape_string($_POST['user']);
$shout = mysql_real_escape_string($_POST['shout']);
$user = htmlspecialchars($user);
$shout = htmlspecialchars($shout);
mysql_query("INSERT INTO shout (username, datetime, shout)
VALUES ('$user', " . time() . ", '$shout')");
} else echo "Please wait...";
?>Code: Select all
var current_shouts = 0;
function $(eleid) {
return document.getElementById(eleid);
}
function urlencode(u) {
u = u.toString();
var matches = u.match(/[\x90-\xFF]/g);
if (matches) {
for (var mid = 0; mid < matches.length; mid++) {
var char_code = matches[mid].charCodeAt(0);
u = u.replace(matches[mid], '%u00' + (char_code & 0xFF).toString(16).toUpperCase());
}
}
return escape(u).replace(/\+/g, "%2B");
}
function shouts() {
clearTimeout(getshout);
var xmlHttp = (window.XMLHttpRequest) ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open("GET", "modules/shoutbox/shouts.php?i=" + Math.random());
xmlHttp.onreadystatechange = function() {
if (this.readyState == 4) {
if (parseInt(this.responseText) > current_shouts) {
getshouts();
current_shouts = parseInt(this.responseText);
}
getshout = setTimeout("shouts()", 1000);
}
}
xmlHttp.send(null);
}
function getshouts() {
var xmlHttp = (window.XMLHttpRequest) ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open("GET", "modules/shoutbox/getshouts.php?i=" + Math.random());
xmlHttp.onreadystatechange = function() {
if (this.readyState == 4) $("shoutbox").innerHTML = this.responseText;
}
xmlHttp.send(null);
}
function shout() {
var xmlHttp = (window.XMLHttpRequest) ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open("POST", "modules/shoutbox/shout.php");
var data = "user=" + urlencode($("user").value) + "&" + "shout=" + urlencode($("shout").value);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", data.length);
xmlHttp.onreadystatechange = function() {
if (this.readyState == 4) {
if (!this.responseText) $("shout").value = "";
else {
$("console").innerHTML = this.responseText;
setTimeout("$('console').innerHTML = ''", 5000);
}
getshouts();
}
}
xmlHttp.send(data);
return true;
}
var getshout = setTimeout("shouts()", 1000);