Get Our Free Ebook
Beginners Guide to HTML

  Web Programming PHP Message Box in PHP

Message Box in PHP

Alex PHP Feb 01, 2007

message box in phpIn this tutorial you are going to learn how to make a message box in PHP. If you don't understand simple form functions, you should probably read an HTML Goodies tutorial on it, here.

Create a new text document called messages.txt and close it. This will be the document where all the messages are stored.

Copy the following code into a new text document, and save it as shout.php. This is the code that will handle your visitors' comments, and write them to messages.txt, which we created above in the last step.

// The data file where messages are stored.
$dataf = "messages.txt";

// Max length of of messages
$length = 150;

// Number of messages shown.
$comments = 50;

$textsize = 4;

if (!$name)
{ $name = "||"; }
else $name .= ":";

$message = stripslashes($message);

$wrap = intval((20)/($textsize-2))+1;

$message = wordwrap($message, $wrap, ' ', 1);

$comfile = file($dataf);if ($message != "") {$df = fopen ($dataf, "w");

fwrite ($df, "<a href="mailto:$email">@</a>
<a href="/img_articles/10747/$url" target="_blank"><b> $name</b></a>
for ($i = 0; $i < $comments; $i++) {fwrite ($df, $comfile[$i]);}fclose($df);}
Header("Location: $HTTP_REFERER");

Ok now you need to make a form so people can actually post in your message box. Here is my sample form. I suggest you use it for now, because it works. Later in this tutorial I am going to teach you how to change/add more text fields.

<form method="POST" action="shout.php">
<table width="100%">
<td><input size="17" maxlength=18 type="text" name="name"></td>
<td><input size="17" maxlength="35" type="text" name="url" value="http://"></td>
<input size="17" maxlength="35" type="text" name="email" value="[email protected]">
<td><input size="17" maxlength="150" type="text" name="message"></td>
<td colspan="2">
<input type="submit" value="Shout">
<input type="reset" value="Clear">

Alright now all you have to do is display the messages that your users post. This can be done with a simple php include:

<?php include "messages.txt" ?>

You can also do this with an iframe, but since frames are bad, you should use a PHP include. Anyway, I suggest that you put your messages right under your form, that way people will see your form first and hopefully will post something. Ok now upload everything to your PHP-enabled server. Chmod shout.php and messages.txt to 777. This is the most important step, because it allows shout.php to edit messages.txt, so your messages can be stored. If you don't know what chmodding is, click here. Now go to your page with the form on it in your browser, and test it out. If you like to be organized you could put all the files in their own folder or something...

Editing the Input Fields

You might want to edit the fields that you visitors can fill out, so here is how.

  1. Edit the form and add or delete inputs. Make sure you give each input a name. Remember these names.
  2. Find the line in shout.php that starts with $comfile. It should be long. This is the HTML of how your messages are displayed.
  3. See the variables with $? Those display what your visitors posted.
  4. Remember the names of the inputs you made? Add a $ in front of them, and put them where you want the messages to be displayed.
  5. Now all you have to do is save, upload, and test it!

subscribe to newsletter