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="[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!

1

3 Answers 3

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 = '[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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

<?php $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 = 'Submission! - From: ' . $visitorEmail; $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: de-signs.com.au'); exit; ?>
What's on line 19 in /home2/designsc/public_html/test/send.php?
<?php $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://'); exit; ?>
You forgot the closing bracket } for the else construct after exit;.
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

Comments

0

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 />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.