Creating Shout Box in PHP

Introduction

In this article I explain how to create a shoutbox in PHP. Creating a shoutbox using a MySQL database to store shouts in a table. First of all I will create a simple HTML file and then creating a database. In the database a table is created and the table name is "shoutbox".

Example

This is a HTML file to be saved as form.html:

<html>

<head>

<title>My Shout Box Form</title>

</head>

<body bgcolor="#FCFBDC">

<form action="shoutbox.php" method="post" >

Name:&nbsp;&nbsp;&nbsp;

<input type="text" name="name" size=30 maxlength='100'></input><br/>

Message:

<input type="text" name="message" size=30 maxlength='100'></input><br/>

<input type="submit" name="submit" value="submit"></input>

</form>

</body>

</html>

This is a PHP file; save this file as "shoutbox.php". In this file is a builtin connection file. I have created a database with MySQL to save the shoutbox in our table. In this database you will need to create four fields for Id, name, message and time.

This is the table for storing the shouts:

Shoutbox save in database.jpg 

<?php

mysql_connect("localhost","root","");

mysql_select_db("vinod");

$name=$_POST['name'];

$message=$_POST['message'];

$time= date("F j, Y, g:i a");

$result=MYSQL_QUERY("INSERT INTO shoutbox (id,name,message,time)".

"VALUES ('NULL','$name', '$message','$time')");

$result = mysql_query("select * from shoutbox order by id desc limit 5");

while($r=mysql_fetch_array($result))

{

$time=$r["time"];

$id=$r["id"];

$message=$r["message"];

$name=$r["name"];

echo $name."<br/>".$message."<br/>".$time."<br/>";

}

?>

Output
Shoutbox in php.jpg

Here, you will enter the name and message then press the submit button so your shouts data is saved in your table.

Shoutbox in php1.jpg

Here, show your shouts data fetched from the table.

Shoutbox in php2.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all