0

我的 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.";}

?>
4

4 回答 4

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.";}
于 2013-04-06T12:09:10.747 回答
0

你最终的意思是:empty() 而不是 isset() 因为它总是被设置......但并不总是被填充?

或检查 ifisset($_POST['to'])和其他键,但不要在分配的变量上使用 isset(检查is_null/=== null更好

于 2013-04-06T12:08:00.070 回答
0

如果您的所有表单数据为空,您的代码将发送邮件。 if(!isset($_POST["something"]) 表示如果您的数据未设置,条件过程将触发。

去掉感叹号。

于 2013-04-06T12:14:59.877 回答
0

使用正则表达式 &| 确保您的表单具有正确的验证 提交表单之前的 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'])";
于 2016-10-07T15:59:58.550 回答