简体   繁体   English

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

[英]PHP email not getting sent with if statement

I am trying to use php to mail an email to a user if they check off a checkbox. 我正在尝试使用php如果用户选中了复选框,则将电子邮件发送给用户。 The email doesn't get sent. 电子邮件未发送。 Can you use the ($_POST["receive-access"]) === checked ? 您可以使用($_POST["receive-access"]) === checked吗?

HTML: 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:

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

Your messing up 2 kinds of if statements. 您搞砸了两种if语句。 What your looking for is this: 您想要的是什么:

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

The first statement isset will return true if the value is set. 如果设置了值,则第一个语句isset将返回true And then you have to check if the value equals to checked 然后您必须检查该值是否等于已checked

But then again, if your POST value isset, the checkbox is checked. 但是,如果设置了POST值,则再次选中该复选框。 So the second part of the check isn't needed at all (probably won't work even) 因此根本不需要检查的第二部分(甚至可能无法使用)

So all you have to do is this: 所以您要做的就是:

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

If you need more information about checkboxes and PHP, you could take a look at this website 如果您需要有关复选框和PHP的更多信息,请访问此网站

由于复选框“ retrieve-access”仅在选中时才发送,您可以这样做。

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

Here's how you do it hence checkbox won't exist in post if it is unchecked. 这是您的操作方式,因此,如果未选中该复选框,则该复选框将在帖子中不存在。 Enjoy! 请享用!

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM