phpmailer如何检查字段是否为空sintax [英] phpmailer how to check if field is empty sintax
问题描述
不幸的是,即使其中一些问题看起来很相似,我也找不到其他问题的答案.我是phpmailer的新手,但是我成功使用以下代码通过smtp成功发送了电子邮件.但是,我希望停止发送带有空字段的电子邮件,但是我找不到合适的sintax来做到这一点,对于在字段为空时如何停止发送电子邮件或如何使字段为必填字段,我将不胜感激. (我知道如何进行客户端验证,但是服务器端是一个问题).请参见以下内容:
Unfortunately I could not find the answer in other questions even though some of them seems to be similar. I am new to phpmailer but I managed to succesfully send email succesfully through smtp by using below code. However, I wish to stop sending emails with empty fields but I can't find proper sintax to do it and I would appreciate advise as how to stop email being sent if fields are empty or how to make fields required . (I know how to make client side validation but server side is a problem). Please see below:
<?php
if(isset($_POST['submit'])) {
$message=
'Name: '.$_POST['name'].'<br />
Subject: '.$_POST['subject'].'<br />
Email: '.$_POST['email'].'<br />
Message: '.$_POST['message'].'';
require "PHPMailer-master/class.phpmailer.php";
$mail = new PHPMailer();
require "smtp.php";
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->Subject = "Message from www";
$mail->MsgHTML($message);
$mail->AddAddress("address1@domain.com", " First receipient");
$mail->AddCC("address2@domain.com", "Second receipient");
$result = $mail->Send();
$message = $result ? '<div class="alert alert-success" role="alert"> Message has been sent ! </div>': '<div class="alert alert-danger" role="alert"><strong>Error!</strong> !</div>';
unset($mail);
}
?>
推荐答案
您只需要对if语句进行一些检查即可. empty()
非常适合检查空,空或0字段
You just need to tack on some checks into your if statement. empty()
is great for checking for empty, null, or 0 fields
if(isset($_POST['submit'])
&& !empty($_POST['name'])
&& !empty($_POST['subject'])
&& !empty($_POST['email'])
&& !empty($_POST['message'])) {
您可以在if
语句之前进行一些验证,以确保字段被填充并有效,如果没有,则抛出错误.
You can do some validation before your if
statement to make sure the fields are filled and valid, and throw an error if they aren't.
这篇关于phpmailer如何检查字段是否为空sintax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!