当涉及到php和服务器端脚本时,我很新,所以请光临我。我有一个处理文件来验证并通过电子邮件从我的网站发送联系表格。就发送数据而言,一切都运行良好,但它没有正确处理任何错误。现在,如果必填字段为空,则不应允许发送消息。但就像现在一样,它仍然发送消息。我不知道如何解决这个问题所以任何帮助表示赞赏。
这是我的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,请告诉我。
答案 0 :(得分:4)
您遇到错误时未设置$formerrors=true
,因此始终将其设置为false并始终发送邮件。
尝试这样的事情:
if($myname === '') :
$err_myname = '<div class="error"> Sorry, your name is rquired</div>';
$formerrors=true;
endif;