user3036585
user3036585

Reputation: 1

HTML form working but need a php validation

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="[email protected]" 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 = "[email protected]";
$email_subject = "New Form Submission! - From: " + $visitorEmail;
// edit here
$email_body = "New visitor - $visitorEmail";    
$to = "[email protected]";
$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!

Upvotes: 0

Views: 103

Answers (3)

TiMESPLiNTER
TiMESPLiNTER

Reputation: 5889

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 = '[email protected]';
    $email_subject = 'New Form Submission! - From: ' . $visitorEmail;

    // edit here
    $email_body = 'New visitor - ' . $visitorEmail;    
    $to = '[email protected]';
    $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;
}

Upvotes: 1

aksu
aksu

Reputation: 5235

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

<input type="email" name="email" placeholder="[email protected]" class="cform-text" size="40" title="your email" required />

Upvotes: 0

Tim
Tim

Reputation: 4101

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

Upvotes: 0

Related Questions