PHP电子邮件未通过if语句发送

时间:2015-03-16 17:59:12

标签: php html

我正在尝试使用php将电子邮件发送给用户,如果他们勾选了一个复选框。 电子邮件无法发送。你能使用($_POST["receive-access"]) === checked吗?

HTML:

<tr><td><p style="font-family:latine;">Email: </td><td><input type="email" name="mail" id="mail" style="font-family:latine;" required></p></td>
<br>
</tr>
<tr><td><input type="checkbox" id="receive-access" name="receive-access"></td>
<td><label for="receive-access">Check off this box for access to restricted areas.</label></td></tr>

PHP:

<?php
    $message = "Username: username Password: password";
    $recipient = $_POST["mail"];
    if(isset($_POST["receive-access"]) === checked)
    mail($recipient, "Restricted access username and password", $message);
?>

3 个答案:

答案 0 :(得分:1)

你弄乱了2种if语句。你在寻找的是:

if(isset($_POST["receive-access"]) && $_POST["receive-access"] === checked)

如果设置了值,则第一个语句isset将返回true。然后你必须检查值是否等于checked

但是,如果设置了POST值,则会选中该复选框。所以检查的第二部分根本不需要(甚至可能不起作用)

所以你要做的就是:

if(isset($_POST["receive-access"]))

如果您需要有关复选框和PHP的更多信息,可以查看this website

答案 1 :(得分:1)

由于复选框字段&#34; retrieve-access&#34;只有在检查完毕后才能发送。

if(isset($_POST["receive-access"]))

答案 2 :(得分:0)

以下是您的操作方式,因此如果取消选中,则复选框将不存在于帖子中。请享用!

if(isset($_POST['receive-access']) && $_POST['receive-access'] == 'on')
{
   $_POST['receive-access'] = true;
}
else
{
   $_POST['receive-access'] = false;
}

$message = "Username: username Password: password";
$recipient = $_POST["mail"];
if(isset($_POST["receive-access"]) == true)
mail($recipient, "Restricted access username and password", $message);