Index
1. Contact form
2. Shoutbox
3. User system
I may also make a series of CSS tutorials :]
All of these scripts are made by me alone.
If you find any Bugs or errors Don't hesistate to tell me.
You can also edit any of these scripts to you're liking... Aslong as when or if you re-post them, you give me some credit... Which seems fair to me.
Contact form
Okay, This is a really simple contact form, Using PHP for verification.
So let's start.
Name this as contact.php
HTML Code:
<form method="POST" action="process.php">
<!-- This is the start of the form. You'll notice the action is set to process.php you'll find out more about that later in the tutorial -->
<div align="center">
<table border="0" cellpadding="0" width="400">
<!-- Start of the table that contains the form fields -->
<tr
<td width="117">Name:</td>
<td><input type="text" name="name" size="30" /></td>
</tr>
<tr>
<td width="117">Email Address:</td>
<td><input type="text" name="email" size="30" /></td>
</tr>
<tr>
<td width="117">Subject:</td>
<td><select size="1" name="subject">
<option>Please Select</option>
<option>Option 2</option>
<option>Option 3</option>
<!-- To add more options just use the option tags and put whatever you like.. -->
</select></td>
</tr>
<tr>
<td width="117">Message:</td>
<td><textarea rows="7" name="message" cols="30"></textarea></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit Form" name="send" /></td>
</tr>
</table>
<!-- End of the table -->
</div>
</form>
<!-- End of the form -->
I've left some comments in there for newbies :]
Now the form action, it's named as process.php... and will be the file that will processes the form input and sends it to your email address. Let's make this file now.
PHP Code:
<?php
//Open PHP Tags
if($_SERVER['REQUEST_METHOD'] != "GET")
//If the form has been submitted and the method isn't GET
{
$name = $_POST['name'];
$email = $_POST['email'];
$sub = $_POST['sub'];
$message = $_POST['message'];
//Create variables for each of the form fields
$ip = $_SERVER['REMOTE_ADDR'];
//Create a variable for the users IP address
}
//Now we start validating the form so there's no blank entries.
if (!$name || !$email || !$message || $sub == "Please Select")
//If any of the fields above are blank
{
echo("Error, you left all or some of the fields blank! They are listed below:<p>");
//Output an error message
//Now we start validating each field
if (!$name)
{
echo("- The Name field was left blank!<br />");
}
if (!$email)
{
echo("- The Email field was left blank!<br >");
}
if ($sub == "Please Select")
{
echo("- You didn't choose a subject!<br />");
}
if (!$message)
{
echo("- The Message field was left blank!</p>");
}
die("<p>Form not submitted!</p>");
//Kill the process so it doesn't submit blanks.
}
$msg = "
Name:
$name
Email Address:
$email
Message:
$message
";
//Create a variable for the message sent to your email address.
$subject = "Contact form submission ($sub) From $name";
//Create a variable for the message subject.
mail("you@email.com", $subject, $msg);
//mail the form to your email address, edit the address above!
?>
<html>
Thanks or whatever here..
</html>
And that's it...
For some help on PHP, i recommend visiting:
http://www.w3schools.com/php/default.asp
I've put some comments in, To make it easier to understand for newbies...
Shoutbox
Okay, This is a basic shoutbox that uses, PHP and MySQL.
Anyway,
Firstly you need to create a database...
Then run this query in PHPMyAdmin
Code:
CREATE TABLE `shout`(
`id` int(10) NOT NULL auto_increment,
`name` varchar(100) NOT NULL,
`shout` varchar(255) NOT NULL,
`ip` varchar(15) NOT NULL,
PRIMARY KEY(`id`)
);
Now to make shoutbox.php
PHP Code:
<?php
$host = "localhost";
//Database host, normally localhost
$dbname = "EDIT";
//Database Name
$dbuser = "EDIT";
//Database Username
$dbpass = "EDIT";
//Database Password.
mysql_connect("$host", "$dbuser", "$dbpass");
mysql_select_db($dbname);
//Connects to your Database.
function secure($value)
{
$value = strip_tags($value);
$value = addslashes($value);
$value = stripslashes($value);
$value = htmlspecialchars($value);
$value = mysql_escape_string($value);
return($value);
}
//Secure function to help support SQL Server injection.
if(isset($_POST['shout']))
//Checks if the form has been submitted.
{
$name = secure($_POST['name']);
//Variable for the Name field
$shout = secure(nl2br($_POST['msg']));
//Variable for the Shout field
$ip = $_SERVER['REMOTE_ADDR'];
//Varaible for the Users IP address
mysql_query("INSERT INTO `shout` (`name`, `shout`, `ip`) VALUES ('$name', '$shout', '$ip')");
//Inserts the shout into the database.
}
//End the if statement.
$shouts = mysql_query("SELECT * FROM `shout` ORDER BY `id` DESC LIMIT 5");
//Gets the shouts from the database.
echo("<p>");
while($s = mysql_fetch_array($shouts))
{
echo("<strong>" . stripslashes(secure($s[name])) . "</strong><br />" . stripslashes(nl2br($s[shout])) . "<br />");
}
//Displays the shouts
echo("</p>");
echo("<form method=\"post\">
Name:<br />
<input type=\"text\" name=\"name\" /><br />
Shout: <br />
<textarea cols=\"30\" rows=\"5\" name=\"msg\"></textarea><br />
<input type=\"submit\" value=\"Shout!\" name=\"shout\" />");
//The Shout form.
?>
You should save this as shoutbox.php.
Also, Remember to change the settings... Which i have write as "EDIT".
And I've commented almost every line, for the newbies.
I'm putting the user system in a new post, As i don't have enough room.