Page 1 of 1

Can somebody please help me with my code

Posted: Tue Jul 14, 2009 8:15 pm
by insight
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.

Code: Select all

Fatal error: Call to undefined function fcontrol() in /home/insight/public_html/modules/shoutbox/shout.php on line 3
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 :P).

Anyway 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>
config.php

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());
 
?>
shouts.php

Code: Select all

<?php
    require_once("modules/shoutbox/config.php");
    echo implode(mysql_fetch_assoc(mysql_query("SELECT COUNT(id) FROM shout")));
?>
getshouts.php

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));
?>
shout.php

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...";
?>
shouts.js

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);
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.

Re: Can somebody please help me with my code

Posted: Tue Jul 14, 2009 11:01 pm
by Skara
You never defined fcontrol() -- it's not a predefined function.

Code: Select all

function fcontrol() {
   // your code to do something
}
//NOW you can call the function:
fcontrol();

Re: Can somebody please help me with my code

Posted: Tue Jul 14, 2009 11:15 pm
by insight
Skara wrote:You never defined fcontrol() -- it's not a predefined function.

Code: Select all

function fcontrol() {
   // your code to do something
}
//NOW you can call the function:
fcontrol();
I see, well it didn't have anything about that on the tutorial. I wouldn't know how to create the function :cry: .

Is there any way I can do it? I found another link on their site http://www.aspektas.com/blog/flood-control-in-php. Would this have something to do with it?

If not then I don't know what to do. Is there any other good tutorials to create a basic AJAX shoutbox that can send commands without refreshing the page and if the user has javascript disabled then it automatically loads refreshes the page with the new data?