Newsletter signup with regex?

Excuse me if regex is outdated/there is a better way of doing this. Right now I have a newsletter code. it basically has someone enter in their email into the input, hit submit and it emails me their email so I can add it to my address book to alert people of new articles. My question is how can I use regex or some sort of verification to make sure they are entering in email into this form? Also, once its submitted, how can I sort of use javascript to eliminate the form entirely and replace it with “Thank you, you have been subscribed” or something of that nature? Here is the code I have so far, first html and then PHP. The site I’m using it on is reesecodes.com

<form method="post" action="scripts/signup.php"> <input name="Email" class="enter-information" type="search" placeholder="Email address" required> <input type="submit" id="submit" value="SUBSCRIBE" name="Submit">

[code]<?php
$recipient = “ericreese20@gmail.com”;
$subject = “Subscribe”;

$location = “…/index.html”;

$sender = $recipient;

$body .= “Name: “.$_REQUEST[‘Name’].” \n”;
$body .= “Email: “.$_REQUEST[‘Email’].” \n”;

mail( $recipient, $subject, $body, “From: $sender” ) or die (“Mail could not be sent.”);

header( “Location: $location” );
?>[/code]

  1. http://php.net/manual/en/filter.filters.validate.php

  2. You’ll want to put the PHP mailing into a separate file. Then use jQuery $.ajax() to send the data via ajax, get a response, and do stuff via Javascript (e.g. a success message or fail message) depending on what hte PHP returns to the ajax(). There are tutorials for this.

my PHP is in a seperate file already. I’ll look into that link you sent

Why are you using type=“search” for your email address? type=“email” would be more appropriate.

1 Like

I copied the HTML from the search bar and forgot to change it. Thanks for spotting it. I’m still not sure how to implement that validate. PHP isn’t very strong with me

You might find the first of these examples explain how to use filter_var() to validate an email address.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.