How To Create a ShoutBox

Write your "How To" regarding anything you are comfortable with and you feel it will help the forum members.

NOTE :: All threads started here will appear only after the approval from Administrator
Post Reply
bilalghouri
Posts: 56
Joined: Sat Mar 10, 2007 11:36 am

How To Create a ShoutBox

Post by bilalghouri »

Well, first we will start with the shoutbox, after you have all of the codes up, we will break it up and show you how to customize it.

Shoutbox:

<style type="text/css">
<!--
#contentbox {
background: #E5E5E5;
padding: 5px;
width: 195px;
height: 200px;
overflow: auto; }
ul#shoutboxmessage {
margin: 0;
padding: 0;
list-style-type: none;
color: #000000;
font: normal 10px verdana,tahoma,arial; }
-->
</style>
<?php
require_once("config.php");
$name = $_POST['name'];
$message = $_POST['message'];
$ip = $_POST['ip'];
$mlen = strlen($message);
$maxlength = 150;
$date = date("M jS Y");
if ($_POST['submit']) {
if ($name == "") {
echo "<strong>Error: Please enter your nickname.</strong>";
}
else if ($message == "") {
echo "<strong>Error: No message to be sent.</strong>";
}
else if ($mlen > $maxlength) {
echo "<strong>Error: Message too long.</strong>";
}
else {
$db = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname) or die(mysql_error());
mysql_query("INSERT INTO shoutbox(name,message,date,ip) VALUES('$name','$message','$date','$ip')");
}
}
$db = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname) or die(mysql_error());
$query = "SELECT * FROM shoutbox ORDER BY id DESC LIMIT 20";
$result = mysql_query($query);
echo "<div id=\"contentbox\">\n";
echo "<ul id=\"shoutboxmessage\">\n";
while($r = mysql_fetch_array($result)) {
$name = $r['name'];
$name = strip_tags($name);
$message = $r['message'];
$message = strip_tags($message);
echo "<li><strong>>$name</strong>: $message</li>\n";
}
echo "</ul>\n";
echo "</div>\n";
mysql_close($db);
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<strong>Nickname:</strong>

<input type="text" name="name" maxlength="20"><

<strong>Message:</strong>

<textarea name="message"></textarea>

<input type="submit" name="submit" value="Shout It!">
<input type="hidden" name="ip" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>">
</form>
</div>
?>

Save this as shoutbox.php



Configuration:

<?php
$dbhost = "localhost";
$dbname = "DATABASE_NAME";
$dbuser = "USERNAME";
$dbpass = "PASSWORD";
?>

Save this as config.php



SQL query


CREATE TABLE shoutbox (
id INT(10) not null AUTO_INCREMENT,
name VARCHAR(20),
message TEXT,
date VARCHAR(15),
ip VARCHAR(15),
PRIMARY KEY(id)
);


Save all of these in a folder called, sb

Oh, I almost forgot,
put

<?php
include("sb/shoutbox.php");
?>

where you want your shoutbox to be.



Customize

Now if you look at the begining of you shoutbox.php code, you will see this

<style type="text/css">
<!--
#contentbox {
background: #E5E5E5;
padding: 5px;
width: 195px;
height: 200px;
overflow: auto; }
ul#shoutboxmessage {
margin: 0;
padding: 0;
list-style-type: none;
color: #000000;
font: normal 10px verdana,tahoma,arial; }
-->
</style>

Change that to change the look of your shoutbox.
this was the script of shoutbox .
i hope u can fully understan it.

thanks,
bilal


SHAdmin
Posts: 2089
Joined: Sat Dec 18, 2004 11:28 am
Contact:

Post by SHAdmin »

Your "How To" has been approved, and you have been credited 25 points for the contribution.
Post Reply