0

i need a help with this. What i need to do to validate the email in this form? This is a landing page, I want to get the email of a visitor before he/she can visit my content here is this demo.

Can I get some tips or the right code to use for this page?

<form id="contact-form" action="send.php">
<input type="text" name="email" placeholder="you@yourmail.com" class="cform-text" size="40" title="your email" required>
<input type="submit" value="Enter Now" class="cform-submit">
</form>

send.php file:

$visitorEmail = $_GET['email'];
$email_from = "doctordongok@gmail.com";
$email_subject = "New Form Submission! - From: " + $visitorEmail;
// edit here
$email_body = "New visitor - $visitorEmail";    
$to = "doctordongok@gmail.com";
$headers = "From: $email_from \r \n";
$headers .= "Reply-To: $visitorEmail \r \n";    
mail($to, $email_subject, $email_body, $headers);
header("Location: http://www.de-signs.com.au");

Thank you!

DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105

3 Answers3

1

Use the filter_var() function of PHP:

if(filter_var($visitorEmail, FILTER_VALIDATE_EMAIL) === false) {
    // Invalid E-Mail address
} else {
    // OK
}

So to mockup your new script. It will look something like this

$visitorEmail = $_GET['email'];

if(filter_var($visitorEmail, FILTER_VALIDATE_EMAIL) === false) {
    die('Sorry that\'s no valid e-mail address');
} else {
    $email_from = 'doctordongok@gmail.com';
    $email_subject = 'New Form Submission! - From: ' . $visitorEmail;

    // edit here
    $email_body = 'New visitor - ' . $visitorEmail;    
    $to = 'doctordongok@gmail.com';
    $headers = 'From: $email_from' . PHP_EOL;
    $headers .= 'Reply-To: $visitorEmail' . PHP_EOL;    
    mail($to, $email_subject, $email_body, $headers);

    header('Location: http://www.de-signs.com.au');
    exit;
}
TiMESPLiNTER
  • 5,741
  • 2
  • 28
  • 64
0

Your best bet is probably to validate on the client side using a Javascript and on the server side as well using a regular expression on both ends.

Validating an email with Javascript

Community
  • 1
  • 1
Tim
  • 4,051
  • 10
  • 36
  • 60
0

HTML5's new email type for input is here to help you.

<input type="email" name="email" placeholder="you@yourmail.com" class="cform-text" size="40" title="your email" required />
aksu
  • 5,221
  • 5
  • 24
  • 39