Simple Mailing List example
In this section we will create a form for the user to fill in and also we will create a script to log the results into our email database . So without any further ado lets create our form.
Here is the code .
<form action = "joinnewsletter.php" method = "post">
<table>
<tr><td>
Name : </td>
<td><input type = "text" name = "name">
</td></tr>
<tr><td>
Email :
</td>
<td><input type = "text" name = "email">
</td></tr>
<tr><td>
<input type = "submit" value = "join"></td>
<td><input type = "reset" value = "clear"></td></tr>
</table>
</form>
Which will produce our form . Now we will store the user submitted details into our database . Call this joinnewsletter.php and enterthe following code .
<?php
//make a connection
$connection = mysql_connect("localhost" , "iainhendry" , "iain06")
or die("cannot make connection");
//select our database email
$db = mysql_select_db("email" , $connection)
or die("cannot find database");
//insert the values of our form into our database
$sql_query = "INSERT INTO email VALUES (\"$name\",\"$email\")";
$results = mysql_query($sql_query);
//display a thank you message and a link
echo ("Thank you for subscribing to our newsletter<br>");
echo ("<a href = 'index.htm'>Back to index page</a>");
?>
Now enter some data in the form and press submit (preferrably valid data) . Now we will check our table using MySQL , at the command prompt enter this .
SELECT * FROM email;
Here was what was displayed when I entered a coupl of my ffa submission emails addresses.
As you can see the email addresses and name has been added.
In our next part we will be displaying the available email addresses and also showing how to send messages to all addresses in the database.
Back to Part 1 , Forward to Part 3 |