php联系人表单验证(仍在发送无效电子邮件)

php联系人表单验证(仍在发送无效电子邮件),php,forms,validation,sanitization,Php,Forms,Validation,Sanitization,所以我准备了一个表单,建议我验证表单。我找到了一个教程,但似乎仍然有问题,使php的功能 表单的html: <div id="FGSform"> <form action="/working/wp-content/themes/NEW/mail.php" method="post" name="contactFGS" id="contactFGS"> <ul> <li> <label for="first-name

所以我准备了一个表单,建议我验证表单。我找到了一个教程,但似乎仍然有问题,使php的功能

表单的html:

<div id="FGSform"> 
<form action="/working/wp-content/themes/NEW/mail.php" method="post" name="contactFGS"          id="contactFGS">
<ul>
<li>
    <label for="first-name">First Name</label>
<br>
    <input type="text" id="firstname" name="firstname" required aria-required="true">
</li>
<br>
<li>
    <label for="last-name">Last Name</label><br>
    <input type="text" id="lastname" name="lastname" required aria-required="true">
</li>
<br>
<li>
    <label for="email">Email</label>
<br>
    <input type="email" id="email" name="email" required aria-required="true">
</li>
<br>
<li>
  <label for="contact-reason" id="reason" name="reason">Reason for Contact</label>
      <select id="reason" name="reason" required>
      <option value="."></option>
      <option value="Print Services">Print Services</option>
      <option value="Design Services">Design Services</option>
      <option value="Employment">Employment</option>
      <option value="Questions">Questions</option>
      <option value="Other">Other</option>     
      </select> 
</li>
<br>
<li>
  <label for="comments">Comments</label>
<br>
    <textarea name="contactcomments" id="contactcomments" cols="40" rows="10" required></textarea>
</li> 
<br>
<li>
    <input type="radio" id="newsletter" name="newsletter">
    <label for="signmeup">Sign me up for newsletter, updates and other information about FGS</label>  
</li>
<br>
<li>
<input type="submit" value="Send" name="submit">
</li>

  • 名字

  • 姓氏

  • 电子邮件

  • 联系原因 印刷服务 设计服务 就业 问题 其他

  • 评论

  • 为我注册有关FGS的时事通讯、更新和其他信息

以下是php:

<?php
/*Validate and Sanitaize */

    if (isset($_POST['submit'])){

}

if ($_POST['firstname'] != "") {
    $_POST['firstname'] = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
    if ($_POST['firstname'] == "") {
        $errors .= 'Please enter a valid name.<br/><br/>';
    }       
} else {
    $errors .= 'Please enter your name.</br>';
}   

if ($_POST['lastname'] != "") {
    $_POST['lastname'] = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);
    if ($_POST['lastname'] == "") {
        $errors .= 'Please enter a valid name.<br/><br/>';
    }       
} else {
    $errors .= 'Please enter your last name.</br>';
}

if ($_POST['emial'] != "") {
    $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
    if (!filter_var($email, FILTER_VALITDATE_EMAIL)) {
        $errors .="$email is <strong>NOT</strong> a valid email address.<br/<br/>";
    }
} else {
    $errors .= 'Please enter your email address.<br/>';
}

if (isset($_REQUEST['reason']) && $_REQUEST['reason'] =='.') {
    echo 'Please select a reason for contacting.<br/>';
}

if ($_POST['contactcomments'] != "") {  
    $_POST['contactcomments'] = filter_var($_POST['contactcomments'], FILTER_SANITIZE_STRING);
    if ($_POST['contactcomments'] == "") {
        $errors .='Please enter a message to send.<br/>';
    }
} else {
    $errors .='Please enter a message to send.<br/>';
}





 /* Email Variables */
 $emailSubject = 'Website Mail!'; 
$webMaster = 'email@here.com';



 /* Data Variables */
 $firstname = $_POST['firstname'];
 $lastname = $_POST['lastname'];
$email = $_POST['email'];
$reason = $_POST['reason'];
$contactcomments = $_POST['contactcomments'];
$newsletter = $_POST['newsletter'];





$body = <<<EOD
<br><hr><br>
Name: $firstname <br>
Last Name: $lastname <br>
Email: $email <br>
Reason: $reason <br>
Comments: $contactcomments <br>
Newsletter = $newsletter <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body,
$headers);


/* Results rendered as HTML */
$theResults = <<<EOD
<html>
<head>
<title>sent message</title>
 <meta http-equiv="refresh" content="3;URL=http://mywebsite.com/working/?       page_id=8">
<style type="text/css">
<!--
body {
background-color: #fff; 
font-family: Arial, Helvetica, sans-serif;
font-size: 20px;
font-style: normal;
line-height: normal;
font-weight: normal;
color: #555555;
text-decoration: none;
padding-top: 200px;
margin-left: 150px;
width: 800px;
}
-->
</style>
</head>
<div align="center">Thank you! We will contact you back as soon as posible.</div>
</div>
</body>
</html>
EOD;
echo "$theResults";
?>

我遇到的问题是,一个人可以提交无效的电子邮件,他们也可以选择无效的选择项

我将表单的操作连接到php文件,但我不确定是否需要让每个表单元素调用php文件的特定if/then语句

我是php新手,因此这已被证明是一个真正的挑战


感谢所有帮助您的人。

除了其他拼写错误和逻辑错误(请参阅Fred-ii-)的答案和评论外,您似乎发现了错误,但不要对此采取任何措施

目前,在伪代码中:

Check for errors.
If there are any errors, add them to a message.

Regardless of the possible errors, send the email.
应该是这样的:

Check for errors.
If there are any errors, add them to a message.

Check to see if there is any error message(s)
If yes, complain loudly and exit
Otherwise, send the email.

发现错误只是成功的一半!如果可以的话,你需要纠正它们,如果不能的话,你需要用其他方式来处理它们

应该是筛选验证电子邮件,而不是筛选有效日期电子邮件

您有几个选择。
在您的表格中,在

然后更改此
if(设置($请求['reason'])和&$请求['reason']='.'){

如果(!isset($_请求['reason']){
并且该选项将工作(已测试)

确保更改
if($\u POST['emial']!=“”){

如果($_POST['email']!=”)的话,{

还有迪米特里·莫斯特里的回答

您也可以尝试您已经拥有的功能,但在末尾添加
exit;
,并在您的
中添加
,如果设置了

注意添加的
,它不在处理程序中,需要它。 否则,使用
if(isset
),您将告诉您“如果已设置”,而它未设置

if (!isset($_REQUEST['reason']) || $_REQUEST['reason'] =='.') {
    echo 'Please select a reason for contacting.<br/>';

exit;


这里有一个完整的重写: 注意:最好将变量放在顶部,而不是放在下面。 我添加了
$email=$\u POST['email'];
if(isset($\u POST['submit']){



if($\u POST['emial']!=”){我在你的代码中看不到任何东西会阻止在出现错误时发送邮件。它总是发送。似乎发送邮件应该依赖于
$errors
@showdev OP的(发布代码)
if($\u POST['emial']!=”){
它应该读作
if($\u POST['email']!=”){
注意“emial”在OP的代码中被错贴了。@Fred ii-是的,我看到了你的答案。我的评论与那个打字错误无关。在你的表格中,去掉
value=“.”“
中,然后将这个
如果(isset($\u REQUEST['reason'])和$\u REQUEST['reason']==”){
更改为
如果(!isset($\u REQUEST['reason']){/code>,这个选项会起作用(测试)很好,迪米特里。(+1)就我而言;-)干杯谢谢你!糟糕的打字加上缺乏知识造成了糟糕的局面,但这解决了它。@user2701059不客气,很高兴它成功了。我认为这是一个可以接受的答案。干杯:)@user2701059那是我的朋友,我们称之为“经验”"。每个人都是这样学习的,包括我自己。继续;-)@user2701059你确实应该向我解释一下为什么你不接受我的答案。毕竟我帮助了你。我不介意帮助,我花在这上面的时间和你转身。那根本不对。对不起,当你把这个网页放进网站时,我正试图从谷歌搜索中删除关闭搜索,这是正确的,我只需要像网站删除。这不是对你的人身攻击。
if ($_POST['email'] != "") {


    $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors .="$email is <strong>NOT</strong> a valid email address.<br/<br/>";
    }
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
  {
  echo "E-mail is not valid";

exit;

  }
<?php
/*Validate and Sanitize */

    if (isset($_POST['submit'])){

    $email = $_POST['email'];

}

$error = ""; // added by me

if ($_POST['firstname'] != "") {
    $_POST['firstname'] = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
    if ($_POST['firstname'] == "") {
        $errors .= 'Please enter a valid name.<br/><br/>';
    }       
} else {
    $errors .= 'Please enter your name.</br>';
}   

if ($_POST['lastname'] != "") {
    $_POST['lastname'] = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);
    if ($_POST['lastname'] == "") {
        $errors .= 'Please enter a valid name.<br/><br/>';
    }       
} else {
    $errors .= 'Please enter your last name.</br>';
}


/*
if ($_POST['email'] != "") {


    $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors .="$email is <strong>NOT</strong> a valid email address.<br/<br/>";
    }
}
*/

if(!filter_var($email, FILTER_VALIDATE_EMAIL))
  {
  echo "E-mail is not valid";

exit;

  }

else {
    $errors .= 'Please enter your email address.<br/>';
}

if (!isset($_REQUEST['reason']) || $_REQUEST['reason'] =='.') {
    echo 'Please select a reason for contacting.<br/>';

exit;
}

if ($_POST['contactcomments'] != "") {  
    $_POST['contactcomments'] = filter_var($_POST['contactcomments'], FILTER_SANITIZE_STRING);
    if ($_POST['contactcomments'] == "") {
        $errors .='Please enter a message to send.<br/>';
    }
} else {
    $errors .='Please enter a message to send.<br/>';
}


 /* Email Variables */
 $emailSubject = 'Website Mail!'; 
$webMaster = 'kmurray@frgraphicsolutions.com';


 /* Data Variables */
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$reason = $_POST['reason'];
$contactcomments = $_POST['contactcomments'];
$newsletter = $_POST['newsletter'];


$body = <<<EOD
<br><hr><br>
Name: $firstname <br>
Last Name: $lastname <br>
Email: $email <br>
Reason: $reason <br>
Comments: $contactcomments <br>
Newsletter = $newsletter <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);

/* Results rendered as HTML */
$theResults = <<<EOD
<html>
<head>
<title>sent message</title>
<meta http-equiv="refresh" content="3;URL=http://frgraphicsolutions.com/working/?page_id=8">

<style type="text/css">
<!--
body {
background-color: #fff; 
font-family: Arial, Helvetica, sans-serif;
font-size: 20px;
font-style: normal;
line-height: normal;
font-weight: normal;
color: #555555;
text-decoration: none;
padding-top: 200px;
margin-left: 150px;
width: 800px;
}
-->
</style>
</head>
<div align="center">Thank you! We will contact you back as soon as possible.</div>
</div>
</body>
</html>
EOD;
echo "$theResults";
?>