-1

我有一个处理文件来验证我的网站上的联系表并通过电子邮件发送。就发送数据而言,一切正常,但它没有正确处理任何错误。现在,如果必填字段为空,则不应发送消息。但就像现在一样,它仍然发送消息。我不确定如何解决此问题,因此不胜感激。

这是我的php

    <?php 
if(($_SERVER['REQUEST_METHOD'] =='POST') && (!empty ($_POST['action']))):

if (isset ($_POST['myname'])){$myname=$_POST['myname'];}
if (isset ($_POST['myphone'])){$myphone=$_POST['myphone'];}
if (isset ($_POST['myemail'])){$myemail=$_POST['myemail'];}
if (isset ($_POST['job'])){$job=$_POST['job'];}
if (isset ($_POST['comments'])){
    $comments= filter_var($_POST['comments'], FILTER_SANITIZE_STRING);}

$formerrors = false;



      if($myname === '') :
      $err_myname = '<div class="error"> Sorry, your name is rquired</div>';
      endif;    

      if($myphone === ''):
      $err_myphone = '<div class="error"> Sorry, your phone number is rquired</div>';
      endif;    

      if($myemail === ''):
      $err_myemaile =  '<div class="error"> Sorry, your email is rquired</div>';
      endif;    

      if($job === ''):
      $err_job =  '<div class="error"> Sorry, your business is rquired</div>';
      endif;    



if (!($formerrors)) :
$to = "email@email.com";
$subject = " Request from $myname";

$message = "A new show and tell request from:\n
            $myname \n
            $myemail\n
            $myphone\n
            $job\n
            $comments\n";
$replyto = "From: $myemail";


if(mail($to, $subject, $message)):
    $msg ="Thanks for filling out our form";
    else:
    $msg = "There was a problem sending the message";
    endif; //mail data

endif; //check errors

endif; //form submitted

?>

如果我需要发布更多信息,例如实际的 html,请告诉我。

4

1 回答 1

4

遇到错误时您没有设置$formerrors=true,因此始终设置为 false 并且始终发送邮件。

尝试这样的事情:

if($myname === '') :
  $err_myname = '<div class="error"> Sorry, your name is rquired</div>';
  $formerrors=true;
endif;    
于 2013-07-19T20:00:12.340 回答