yaakov
yaakov

Reputation: 4665

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. The email doesn't get sent. Can you use the ($_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);
?>

Upvotes: 0

Views: 62

Answers (3)

dminones
dminones

Reputation: 2296

Since the checkbox field "retrieve-access" will be only sent if is checked you could just do this.

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

Upvotes: 1

JiNexus
JiNexus

Reputation: 2844

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

Upvotes: 0

Mathlight
Mathlight

Reputation: 6653

Your messing up 2 kinds of if statements. 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. And then you have to check if the value equals to checked

But then again, if your POST value isset, the checkbox is checked. 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

Upvotes: 1

Related Questions