PHP & MySQL, Demos, Tutorials

This newsletter tutorial will show you how to create a newsletter signup form, part 2 will show you how to send out newsletters from your list of users stored in a database.

First we need a database table to store the signups create the following table in phpmyadmin:

CREATE TABLE IF NOT EXISTS newsletter (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;

The table stores a name and email address which can then be used to send out emails. The next part is to create the signup form, this is a standard form with a name and email fields the action is blank as the same page will process the form, the method is set to post so send the name and email securely.

<form action="" method="post">
<p><label>Name</label><input name="name" type="text" size="40" value="" /></p>
<p><label>Email</label><input name="email" type="text" size="40" value="" /></p>
<p><input type="submit" name="submit" value="Signup" /></p>
</form>

To process the form we first check to see if the form has been submitted, this is done by using a function called isset then passing it the submit button name (in this case I called it submit).

if(isset($_POST['submit'])){

If the form has been submitted then collect the name and email from the form.

//collect data from form and remove any tags and make safe for database entry
$name = strip_tags(mysql_real_escape_string($_POST['name']));
$email = strip_tags(mysql_real_escape_string($_POST['email']));

Next check the name is long enough and the email is in the correct format.

//check name length
if (strlen($name) < 3) {
    $error[] = 'Name must be more then 3 characters.';
}

// check for valid email address
$pattern = '/^[^@]+@[^srn'";,@%]+$/';
if (!preg_match($pattern, trim($email))) {
    $error[] = 'Please enter a valid email address';
}

If no errors have been created then carry on, otherwise the form stops here and the errors would be shown. Then insert the name and email address into the database table then show a success message or show an error if there is a problem, then close the two if statements.

// if validation is okay then carry on
if (!$error ) {

//insert into database
$q = mysql_query("INSERT INTO newsletter (name,email) VALUES ('$name','$email')")or die(mysql_error());

//submission successful show a message
echo "<p>Thank you, submission was successful.</p>";

} // end validation
}// end submit

If there has been an error then loop through all error arrays and show them.

//show any errors
if (!empty($error))
{
        $i = 0;
        while ($i < count($error)){
        echo "<div class="msg-error">".$error[$i]."</div>";
        $i ++;}
}// close if empty errors

In it's simplest form that's all there is to making a newsletter signup, here's the full script:

<?php
//include database connection details
require_once('config.php');

if(isset($_POST['submit'])){

//collect data from form and remove any tags and make safe for database entry
$name = strip_tags(mysql_real_escape_string($_POST['name']));
$email = strip_tags(mysql_real_escape_string($_POST['email']));

//check name lengh
if (strlen($name) < 3) {
    $error[] = 'Name must be more then 3 characters.';
}

//if name is equal to name then give an error
if ($name == 'Name') {
    $error[] = 'Please enter your name.';
}

// check for valid email address
$pattern = '/^[^@]+@[^srn'";,@%]+$/';
if (!preg_match($pattern, trim($email))) {
    $error[] = 'Please enter a valid email address';
}

// if validation is okay then carry on
if (!$error ) {

//insert into database
$q = mysql_query("INSERT INTO newsletter (name,email) VALUES ('$name','$email')")or die(mysql_error());

//submission successful show a message
echo "<p>Thank you, submission was successful.</p>";

} // end validation
}// end submit


//show any errors
if (!empty($error))
{
        $i = 0;
        while ($i < count($error)){
        echo "<div class="msg-error">".$error[$i]."</div>";
        $i ++;}
}// close if empty errors

?>

<form action="" method="post">
<p><label>Name</label><input name="name" type="text" size="40" class="box-medium" value="" /></p>

<p><label>Email</label><input name="email" type="text" size="40" class="box-medium" value="" /></p>

<p><label>&nbsp;</label><input type="submit" name="submit" value="Signup" class="button" /></p>
</form>