我有一个简单的HTML/PHP/CSS网站的联系表单,我想让人们发送他们的联系信息,即使他们不提交他们的电子邮件。
但是我试过的所有东西,当我通过联系人表单提交消息时,它要么返回一些错误,要么返回以下错误:"输入的电子邮件地址无效!"
这是我的contactprocess.php
<?php
include("../config.php");
if(!$_POST) exit;
$email = $_POST['Email_Address'];
$error ="";
$errors ="";
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error.="Invalid email address entered!";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('Link','Quantity','Your_Message');
$required = array('Link','Quantity','Your_Message');
$email_subject = "The Message";
$email_content = "new message:'n";
foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != 'company') {
if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."'n";
}
}
if(@mail($your_email,$email_subject,$email_content)) {
header('Location: http://example.com');
} else {
echo 'ERROR!';
}
}
?>
我肯定这是一个简单的编辑,但是我就是看不懂。
你的代码明确地阻止了&添加错误,如果电子邮件是无效的?
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error.="Invalid email address entered!";
$errors=1;
}
然后if($errors==1) echo $error;
else{ // other stuff
如果你不想要这种行为,最简单的选择不是直接删除代码吗?
或者是这个想法,如果他们提交了一个电子邮件,它必须是有效的-否则允许它通过,如果没有。在这种情况下,你的if应该像
if (!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL)) {