0

I have a newsletter subscribe form on my website and i want it to go to a page that

confirms the user has been signed up. At the moment it just refreshes the page and gives

you a little message where the form box was. Having tested it, it also doesn't care whether

you put the email address in wrongly etc. I would like it so it checks this info and only

submits correct email addresses. The code that is there at the moment is

<?php

// get vars
$email_address = strtolower(trim($_REQUEST['email_address']));

if((strlen($email_address) > 0) && (strpos($email_address, "@")))
{
    // add to db
    $newsletterQry = db_query("SELECT * FROM newsletter_subscribers WHERE email='" . mysql_real_escape_string($email_address) . "'");
    if(db_num_rows($newsletterQry) == 0)
    {
        // add
        db_query("INSERT INTO newsletter_subscribers (email, created) VALUES ('" . mysql_real_escape_string($email_address) . "', NOW())");
    }
}

// return back to the index page with confirmation
header("location: ".$_SERVER["HTTP_REFERER"]."?nlMsg=".urlencode("You've been added to the site newsletter."));
exit;
?>
Vishal
  • 8,246
  • 6
  • 37
  • 52

1 Answers1

0

See the solution on How to validate an Email in PHP. You want to sanitize e-mail addresses using either RegEx, or PHP's built-in filter_var() function.

<?php
$email_address = strtolower(trim($_REQUEST['email_address']));
if (filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
    // This email address is valid insert into newsletter 
}
Community
  • 1
  • 1
Highway of Life
  • 22,803
  • 16
  • 52
  • 80
  • thanks for the reply do i need to just insert that line or do i need a bunch of other code to validate it. Also how can i get the confirmation to open a popup or page saying thank you for subscirbing etc – Adam Wilson Mar 19 '13 at 12:57
  • @AdamWilson, that bit of code is all you need to do to validate the email is valid. You could do a reverse to test if it's invalid... `if (!filter_var($email_address, FILTER_VALIDATE_EMAIL)) { ... }` As to to the question of a confirmation or popup, that's beyond the scope of this question (thread). Please search stackoverflow for that answer or create a new question. Also, you might have a read of the StackOverflow FAQ: http://stackoverflow.com/faq – Highway of Life Mar 19 '13 at 19:34