如何在php上验证我的电子邮件提交?

时间:2016-05-26 04:12:45

标签: php html dreamweaver

我正在使用Dreamweaver进行编码并整理网页。我用我的电子邮件编码创建了一个php页面,该页面是从html页面上的表单和提交按钮执行的。我每天都不断收到空白的电子邮件,这显然意味着我需要添加验证编码。我尝试添加编码,但问题仍然存在。即使表格为空白,页面仍然会提交。

以下是我目前的编码。



<?php

if(!filter_var($_POST['CustomerEmail'], FILTER_VALIDATE_EMAIL)) { $valerrors[] = "The email address you supplied is invalid." ;}
$customeremail=$_POST['CustomerEmail'];
$email='test@gmail.com';
$custfirst=$_POST['CustomerFirstName'];
$custlast=$_POST['CustomerLastName'];
$custphone=$_POST['CustomerNumber'];
$custaddress=$_POST['CustomerAddress'];
$custcity=$_POST['CustomerCity'];
$custstate=$_POST['CustomerState'];
$custrequest=$_POST['CustomerService'];

$subject='Service Request';

$body = <<<EOD
<br><hr><br>
Email: $customeremail <br>
First Name: $custfirst <br>
Last Name: $custlast <br>
Phone: $custphone <br>
Address: $custaddress <br>
City: $custcity <br>
State: $custstate <br>
Request: $custrequest <br>
EOD;



$headers = "From: $customeremail\r\n";
$headers .= "Content-type: text/html\r\n";
$Send = mail($email, $subject, $body, $headers);
$confirmation = <<<EOD
<html>"Thanks for Submitting."</html>
EOD;
echo"$confirmation";



?>
&#13;
&#13;
&#13;

我可能会将if语句放在错误的位置。有人可以更正我的编码,因此如果客户电子邮件留空,则不会加载确认页面并且不会发送电子邮件吗?

3 个答案:

答案 0 :(得分:0)

你需要像这样检查

if (filter_var('abc@gmail.com', FILTER_VALIDATE_EMAIL)) { 
    echo 'VALID'; 
} else {
    echo 'NOT VALID';
}

见这里:http://php.net/manual/en/function.filter-var.php

答案 1 :(得分:0)

您可以在开头初始化$valerrors = [];并使用以下条件:

if (empty($valerrors)) { 
    $headers = "From: $customeremail\r\n";
    $headers .= "Content-type: text/html\r\n";
    $Send = mail($email, $subject, $body, $headers);
    $confirmation = <<<EOD
    <html>"Thanks for Submitting."</html>
    EOD;
}

请注意,您应对所有用户输入应用类似的验证。

答案 2 :(得分:0)

我想你可以添加&#34; required&#34;以您的表单Html

输入您的电子邮件输入
<input type="email" name="CustomerEmail" required>

因此,在您提供值

之前,Html不允许您提交