Email Subscription List in PHP/HTML

Introduction: This tutorial is on how to create a simple email subscription service - part 1 - allowing the user to enter their email address. HTML: Before we can process any information the user enters and then add it to our database of email subscription addresses, we need to take the information from the user - for this we need a simple HTML form.
  1. <head>
  2. </head>
  3. <body>
  4. <form action='page.php' method='POST'>
  5. <span>Email: </span><input type='email' name='email' placeholder='Email here' />
  6. <input type='submit' value='Submit' name='submit' />
  7. </form>
  8. </body>
  9. </html>
We now have a simple HTML form for the user to enter their email address in to, along with a submit button the user can click to submit their email address via the form, ready for processing - of course, the user can still simply press the enter button on their keyboard while inside the email input box and the form will submit. Processing: As you can see on our above HTML form, the 'action' is set to 'page.php' - this means that the page 'page.php' will be where the form data is sent, and therefore that is where we need to process the data. This 'page.php' can be the same page as the HTML form above. The first thing we want to do in our 'page.php' file is to create some basic PHP tags...
  1. <?php
  2.  
  3. ?>
Next we want to ensure that the form has been submitted - we can do this through checking is a POST variable of 'email' is set (this is because the form's 'method' is 'POST', and the name of the input email box is 'email')...
  1. if (isSet($_POST['email'])) {
  2. //is set
  3. }else
  4. echo 'Please set an email address via the HTML form first.';
Now we have a simple error message in case the email address it not set. Next we want to ensure that the email address is not null (null, in programming terms, means empty)...
  1. if ($_POST['email'] != '') {
  2.  
  3. }else
  4. echo 'No email address was entered!';
Now that we are sure a valid email address has been entered we can first throw the entered value in to a variable, and then use that variable in a query to our database...
  1. $email = mysqli_real_escape_string($_POST['email']);
We use the 'mysqli_real_escape_string' function to add some security to our HTML form - this strips the entered string of possible malicious content. Database Querying: Now we are ready to move on to our database. First we want to create a database with some tables, I will create my database to be named 'flnEmails', and in there a table named 'emails' with the following columns... id, integer, 5 length, primary key, auto increment email, varchar, 255 length Once we have created our tables, we can now create a PHP MySQLi query to check if the email address is already entered in to our database subscription list...
  1. $con = mysqli_connect('localhost', 'root', '', 'flnEmails');
  2. $q = mysqli_query($con, "SELECT * FROM `emails` WHERE `email`='$email'");
In the above script, the $con object/variable is set to a connection to our database using the information (server, username, password, database name). After that, we use our connection object to query our database to return all of the rows where the column 'email' is the same value as the currently processing email address ('$email'). Now we can count the amount of returned rows, if it's over zero (0), we know that the email is already entered and it should not be re-added to our table...
  1. if (mysqli_num_rows($q) > 0) {
  2. echo 'Email address already entered.';
  3. }else{
  4. //Add email address here.
  5. }
Finally we want to add our email address to our database table, we do this through an MySQLi 'INSERT INTO' query...
  1. mysqli_query($con, "INSERT INTO `emails` VALUES ('', '$email')");
Here, we are inserting {null} (this is because it is the 'auto increment'ing column 'id') followed by the currently processing email address, in to our table named 'emails'. Finished! Here is the full source code...
  1. <?php
  2. if (isSet($_POST['email'])) {
  3. if ($_POST['email'] != '') {
  4. $con = mysqli_connect('localhost', 'root', '', 'flnEmails');
  5. $q = mysqli_query($con, "SELECT * FROM `emails` WHERE `email`='$email'");
  6. if (mysqli_num_rows($q) > 0) {
  7. echo 'Email already entered.';
  8. }else
  9. mysqli_query($con, "INSERT INTO `emails` VALUES ('', '$email')");
  10. }else
  11. echo 'Email is not valid.';
  12. }else
  13. echo 'Email not set.';
  14. ?>
  15. <html>
  16. <head>
  17. </head>
  18. <body>
  19. <form action='page.php' method='POST'>
  20. <span>Email: </span><input type='email' name='email' placeholder='email here' />
  21. <input type='submit' name='submit' value='Submit' />
  22. </form>
  23. </body>
  24. </html>

Comments

Submitted byJimmmyuasdfwe (not verified)on Tue, 08/28/2018 - 05:10

Where does this go in the script? $email = mysqli_real_escape_string($_POST['email']); You didn't include it and the script doesn't work. Please fix it.

Add new comment