Nov 8, 2009
Pages: 1, 2

Php Tutorial: Making A Shoutbox - Requirements: PHP, MySQL

free web hosting

Read Latest Entries..: (Post #14) by cartune on Jan 18 2009, 08:46 PM.
ah thank you... this will be perfect for my site
Read the FIRST post of this Topic. - Express your Opinion! Contribute Knowledge :-).

Open Discussion & Free Web Hosting > Computers & Tech > How-To's and Tutorials > Websites and Web Designing

Php Tutorial: Making A Shoutbox - Requirements: PHP, MySQL

Habble
Hi everyone, I'm going to tell you how to make a simple shoutbox using PHP and MySQL.

To start off, open up mysql in the command line, or phpmyadmin, and create a database called shoutbox.
Next, enter the following sql into the command line, or the phpmyadmin sql box, while using the shoutbox database:
CODE
create table messages(author varchar(30), message text, time timestamp, mid int auto_increment, primary key(mid));

This creates the table we need to store the messages, note the "mid" column, this gives each message a seperate id number, we'll see why that's useful later.
Next, create a new PHP page on your server, lets call it shoutbox.php. Copy this code into it:
CODE
<html>
<head>
<title>My Shoutbox</title>
</head>
<body>
<center><b>My Shoutbox</b></center>
<center><table width="80%">
<!-- Shoutbox update -->

<!-- Shoutbox update -->
<!-- Shoutbox posts -->

<!-- Shoutbox posts -->
<!-- Shoutbox form -->

<!-- Shoutbox form -->
</table></center>
</body>
</html>

Ok, now we start on the actual shoutbox.
In between the "<!-- Shoutbox form -->" lines, type the following text, we're going to make the shoutbox form.
CODE
<tr><td><form name="shoutbox" action="?action=shout" method="POST"><label for="name">Name:</label><input type="text" name="name"><br>
<textarea name="message" cols="20" rows="2"><br><input type="submit" value="Shout!"></form></td></tr>

Ok, this gives us a place to write a name, and a message. Note that in the "action" attribute of the form, I've put "?action=shout", if the page is called shoutbox.php, it will take us to shoutbox.php?action=shout. This uses a GET variable, which we will talk about below.
Now, we're going to make the section that updates the shoutbox. This is where we get into some actual PHP coding! Between the "<!-- Shoutbox update -->" lines, type the following text:
CODE
<?php
mysql_connect("hostname","username","password");
mysql_select_db("shoutbox");
if ($_GET["action"]=="shout")
{
$update = "insert into messages values('".$_POST["name"]."','".$_POST["message"]."',NULL,NULL);";
mysql_query($update) or die("Failed to update database. The error returned was:<br>".mysql_error());
}
?>

Not much, is it? But it does what we need. First, it connects to mysql. Change the text in bold to your own mysql hostname, username and password. Then, it selects the database. If your database isn't called shoutbox, change this bit.
Then, it checks to see if the GET variable action is set to "shout." Get variables are those things you sometimes see in urls, you know, when there's a ?something=something&hello=whatever after the address. These are get variables. In this example, submitting the form takes you to shoutbox.php?action=submit, so the action GET variable is set to "shout."
Next, it performs the MySQL query, which inserts the data from the form into the shoutouts table. There's also a bit of code to show us whet the problem is if it doesn't work.
Ok, now for the final bit, the posts. In between the "<!-- Shoutbox posts -->" lines, write the following text:
CODE
<?php
$query = "select * from messages order my mid desc";
$result = mysql_query($query);
while ($array=mysql_fetch_array($result))
{
echo "<tr><td>".$array["message"]."<br><font size=\"small\">Posted on ".date("D j/n/Y g:i:s a",$array["time"])." by ".$array["name"]."</font></td></tr>";
}
?>

Ok, what this does, is it starts off by getting the data from the database, and ordering it by it's unique id, descending. It then writes the information into a table cell once for every entry in the table. The date function makes a readable date out of the time from the database.
By now, the page should look like this:
CODE
<html>
<head>
<title>My Shoutbox</title>
</head>
<body>
<center><b>My Shoutbox</b></center>
<!-- Shoutbox update -->
<?php
mysql_connect("hostname","username","password");
mysql_select_db("shoutbox");
if ($_GET["action"]=="shout")
{
$update = "insert into messages values('".$_POST["name"]."','".$_POST["message"]."',NULL,NULL);";
mysql_query($update) or die("Failed to update database. The error returned was:<br>".mysql_error());
}
?>
<!-- Shoutbox update -->
<center><table width="80%">
<!-- Shoutbox posts -->
<?php
$query = "select * from messages order my mid desc";
$result = mysql_query($query);
while ($array=mysql_fetch_array($result))
{
echo "<tr><td>".$array["message"]."<br><font size=\"small\">Posted on ".date("D j/n/Y g:i:s a",$array["time"])." by ".$array["name"]."</font></td></tr>";
}
?>
<!-- Shoutbox posts -->
<!-- Shoutbox form -->
<tr><td><form name="shoutbox" action="?action=shout" method="POST"><label for="name">Name:</label><input type="text" name="name"><br>
<textarea name="message" cols="20" rows="2"><br><input type="submit" value="Shout!"></form></td></tr>
<!-- Shoutbox form -->
</table></center>
</body>
</html>

And there you have it! Your very own shoutbox! Feel free to style is, and modify it anyway you want. You can add validation code, and functions to delete posts!
- Jay

 

 

 


Comment/Reply (w/o sign-up)

vidit
Thankx a lot. But i have a doubt, i have seen many free shoutbox script available on the web, but while shouting the iframe containing the shoutbox is refreshed. While on some forums like here there is no refreshing done, rather it gets shouted in a cool way and instantly.
Is there any free script for such a cool shoutbox?

Comment/Reply (w/o sign-up)

Chesso
It would be good if you added an alternate explanation of how you would build the database/tables(s) through the interface method of phpMyAdmin (rather than executing a direct mySQL query). Just a thought smile.gif.

Ummm self refreshing shoutboxes, I think mine does that, or maybe it refreshes the page, I can't remember exactly but it updates pretty much on the fly.

Comment/Reply (w/o sign-up)

nightfox
QUOTE(Chesso @ Jul 2 2007, 08:49 PM) *
It would be good if you added an alternate explanation of how you would build the database/tables(s) through the interface method of phpMyAdmin (rather than executing a direct mySQL query). Just a thought smile.gif .

Ummm self refreshing shoutboxes, I think mine does that, or maybe it refreshes the page, I can't remember exactly but it updates pretty much on the fly.

Yeah, those are very good points. Maybe even just setting up the tables in a text file wink.gif. As for a self-refreshing shoutbox, you can include a meta-refresh or an AJAX script that looks for changes in the database when it's submitted and then it refreshes... sorta like the Digg Spy utility.

[N]F

 

 

 


Comment/Reply (w/o sign-up)

Habble
Yeah, I use JavaScript for mine, I'll dig it out of the code for you
Ok, place this anywhere on the page.
CODE
<script language="JavaScript">

var refreshinterval=120

var displaycountdown="yes"

var starttime

var nowtime

var reloadseconds=0

var secondssinceloaded=0

function starttime()

{

starttime=new Date()

starttime=starttime.getTime()

countdown()

}

function countdown()

{

nowtime= new Date()

nowtime=nowtime.getTime()

secondssinceloaded=(nowtime-starttime)/1000

reloadseconds=Math.round(refreshinterval-secondssinceloaded)

if (refreshinterval>=secondssinceloaded)

{

var timer=setTimeout("countdown()",1000)

if (displaycountdown=="yes")

{

window.status="Page refreshing in "+reloadseconds+ " seconds"

}

}

else

{

clearTimeout(timer)

window.location.reload(true)

}

}

window.onload=starttime

</script>

Change refreshinterval to however many seconds you want it to wait before it reloads, also, it tells you how many seconds left until it reloads in the status bar, so it doesnt catch you by surprise and make you write the message all over again.
Also, I'll have a fiddle with PHPMyAdmin tonight, and write up something on how to create the tables in it, but for the meantime, you can just go to the "SQL" tab in phpmyadmin (It's there in every version I've used) and copy and paste the SQL code.

Comment/Reply (w/o sign-up)

vidit
Thankx for the "self-refreshing" one. But can anyone provide me the script for a shoutbox like the one here. When we make a shout, it gets shouted like an im and the page(or iframe) don't need to get refreshed. The shoutbox in astahost is very cool. I want the script for a shoutbox for my site, which i expect, that will soon be hosted here smile.gif.

Comment/Reply (w/o sign-up)

Chesso
Self refreshing is the really the only way to go I think?

It usually works out well because the content of the page will be cached anyway, so it's quick, I'm not sure exactly how I do it but it seems to be an immediate on screen update (I'll have to check when I have time), but if things like queries on pages are a problem, try using a form of caching pages to help that across the board if it's feasible.

Comment/Reply (w/o sign-up)

Quatrux
This one here I think is fully made with AJAX technology, I mean the shoutbox on astahost forums.. and it is really a bit more advanced than this simple shoutbox.. I never really liked iframes or any frames, it is really better to use something else these days, especially than using AJAX you can do really a lot of good stuff, a lot can be done using a browser these days biggrin.gif it doesn't show only html anymore like it did in the old days biggrin.gif

Comment/Reply (w/o sign-up)

str8upclownz
whats the best use for a shoutbox, ive never used one before

Comment/Reply (w/o sign-up)

Habble
Shoutboxes are like chat-rooms. you can put it on a website, and people can chat with them. Like the one at the top of the forum.

Comment/Reply (w/o sign-up)

Latest Entries

cartune
ah thank you... this will be perfect for my site smile.gif

Comment/Reply (w/o sign-up)

ryantommo
ty i will put this on my tbg site

Comment/Reply (w/o sign-up)

nullvalue
this is my very first footage on this site.. hope i learn a lot from you guuys.. and very firstly, tnx for the post.. i'm really searching for this oNe..

Comment/Reply (w/o sign-up)

rockershive
what's the best site where I can find step-by-step instructions of installing php, mysql, dreamweaver, etc...

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Pages: 1, 2
Similar Topics

Keywords : php, tutorial, making, shoutbox, requirements, php, mysql

  1. Ajax+php+sql=simply Superb!(with Visitor Tracking) :: Section 2 (retrive Values From Database And Dynamic Update!)
    A small tutorial to explain integrating php, ajax and MySQL Database (4)
  2. Php/xhtml Pages
    A simple guide to making valid PHP/XHTML pages (3)
    As you know, if you use the XML encoding tag, XHTML Doctype and the XML Namespace attribute in the
    html tag on a .php document, it won't render properly and will infact display errors. A
    solution to this problem is to use the require function of the PHP system. (I'm new at PHP,
    just learnt it yesterday, please feel free to correct anything /smile.gif"
    style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />) Here are some examples of
    pre-body text: Normal XHTML document pre-body information: CODE My XHTML Site
    ...style inf....
  3. CuteNews: PHP-based Blog System - No MySQL
    (11)
    I don't know if you guys have heard about CuteNews, but I think it's an awesome blogging
    system. If you don't know where to get it or how to set it up, here is a quick run-down.
    Download the zip file (virus free) from http://www.mysharebox.com/dl.php?key=8276639 . 1) Unzip
    the file. 2) Make a folder titled "cutenews". 3) Upload the contents of the cutenews folder into the
    one you've just created. 4) CHMOD the file index.php to 777. 5) CHMOD the folder "data" to 777.
    6) Then CHMOD all the files and folders inside of "data" to 777. 7) Go to http://YO....
  4. Making A Website
    Along with Some Dos & Don'ts (2)
    I had originally had this posted on my domain at nevernormal.com, and thought that you guys could
    use it here as well. Granted, this is geared to the uber newbie, so don't razz me if I
    don't suggest the most advanced in web design. lol QUOTE So, you want to make a
    website? 1. First, think about what you want your site to be about. There are fanfic sites, like
    Drastic Measures and fanfiction.net ; cliques or clubs, like the BtVS Writers' Guild ; or, if
    you want, you could have a general site, whether it be about a show/movie you like, or even j....

    1. Looking for php, tutorial, making, shoutbox, requirements, php, mysql

See Also,

*SIMILAR VIDEOS*
Searching Video's for php, tutorial, making, shoutbox, requirements, php, mysql
advertisement



Php Tutorial: Making A Shoutbox - Requirements: PHP, MySQL

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com