我的PHP代码出错了。我使用IF-ELSE检查一切正常,但它一直给我“你没有输入收件人”。
<?php
$to=trim($_POST['toperson']);
$from=trim($_POST['spoofrom']);
$message=trim($_POST['message']);
$subject=trim($_POST['subj']);
if (substr_count($to, '@') <= 1 ) {
if (!isset($to)) {
if (!isset($from)) {
if (!isset($subject)) {
if (!isset($message)) {
mail($to, $subject, $message, "From: " . $from);
print "Message was sent!";
}else {print "You did not enter a message";}
}else {print "You did not enter a subject";}
}else {print "You did not enter a from email";}
}else {print "You did not enter a recipient";}
}else{print "You entered 2 or more emails.";}
?>
答案 0 :(得分:1)
尝试
按if (!isset($to))
替换您的条件if (isset($to))
并添加空检查
文档: http://php.net/manual/en/function.isset.php
http://www.php.net/manual/en/function.empty.php
像这样:
if (substr_count($to, '@') <= 1 ) {
if (isset($to) && !empty($to)) {
if (isset($from) && !empty($from)) {
if (isset($subject) && !empty($subject)) {
if (isset($message) && !empty($message)) {
mail($to, $subject, $message, "From: " . $from);
print "Message was sent!";
}else {print "You did not enter a message";}
}else {print "You did not enter a subject";}
}else {print "You did not enter a from email";}
}else {print "You did not enter a recipient";}
}else{print "You entered 2 or more emails.";}
答案 1 :(得分:0)
你最终的意思是:empty()而不是isset(),因为它总是被设置......但并不总是被填充?
或者检查是否isset($_POST['to'])
和其他键,但不要在指定的变量上使用isset(检查is_null
/ === null
更好
答案 2 :(得分:0)
如果您的所有表单数据都是空的,您的代码将发送邮件。
if(!isset($_POST["something"])
表示如果未设置数据,则会触发条件过程。
删除感叹号。
答案 3 :(得分:0)
确保您的表单使用正则表达式&amp; |进行适当的验证表单提交前的html5 / css3。最后使用!empty()verse isset(),我建议把
if (mail($to, $subject, $message, "From: " . $from)) {
print "Message was sent!";
} else { print "email failed to send!"; }
另外,我将From:设置变量时没有调用它,但这更偏向个人。
$from= "From: trim($_POST['spoofrom'])";