0

I've just added a checkbox to my contact form to indicate whether a user would like to sign up to my newsletter or not. I would like my PHP script to output the value of the checkbox when it sends me an email. I have searched the internet thoroughly and can't find any solutions, just people with similar problems. https://joebaileyphotography.com/contact%20me.html

HTML:

<form method="post" name="myemailform" action="form-to-email.php">

<label class="label" for="fname">First Name</label>
<br>
<input type="text" id="fname" name="fname" placeholder="Your name..">
<br>
<label class="label" for="lname">Last Name</label>
<br>
<input type="text" id="lname" name="lname" placeholder="Your last name..">
<br>
<label class="label" for="Email">Email Address</label>
<br>
<input type="text" id="Email" name="Email" placeholder="Your email address...">
<br>
<label class="label" for="subject">Subject</label>
<br>
<textarea id="subject" name="subject" placeholder="Drop me a line..." style="height:200px"></textarea>
<br>
<input type="checkbox" id="subscribeNews" name="subscribe" value="newsletter" checked>
<label for="subscribeNews">Yes, I would like to recieve news from Joe Bailey Photography.</label>
<br>
<br>
<input type="checkbox" id="privacyPolicy" name="privacy" value="privacy">
<label for="privacyPolicy">I consent to having this website store my submitted information so they can respond to my inquiry. For more info, read the <a href="/privacy-policy.html">Privacy Policy.</a></label>
<br>
<br>
<div class="g-recaptcha" data-sitekey="6LeqeRkTAAAAAFGmVFmAorEU9n0yL4NDEpSUnM0R"></div>
<br>
<input type="submit" value="Send Form">

PHP:

<?php
$response = $_POST["g-recaptcha-response"];
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
    'secret' => '6LeqeRkTAAAAABjyRvL0c1vrqG3ZmV51O0-S3xcz',
    'response' => $_POST["g-recaptcha-response"]
);
$options = array(
    'http' => array (
        'method' => 'POST',
        'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$verify = file_get_contents($url, false, $context);
$captcha_success=json_decode($verify);
if ($captcha_success->success==false) {
    echo "<p>Please go back and verify you are human.</p>";
} else if ($captcha_success->success==true) {

    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $visitor_email = $_POST['Email'];
    $subject = $_POST['subject'];

    $email_subject = "Joe Bailey Photography Contact Form";
    $email_body = "You have received a new message from $fname $lname.\n".
    "Here is the message:\n$subject
    ";

    $to = "enquiries@joebaileyphotography.com";
    $headers = "From: $visitor_email \r\n";
    mail($to,$email_subject,$email_body,$headers);
    //done. redirect to thank-you page.
    header('Location: Contact%20Me%20Thank%20You.html#thankyou');
    function IsInjected($str)
    {
    $injections = array('(\n+)',
          '(\r+)',
          '(\t+)',
          '(%0A+)',
          '(%0D+)',
          '(%08+)',
          '(%09+)'
          );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str))
    {
        return true;
    }
        else
    {
        return false;
    }
    }
}
j08691
  • 204,283
  • 31
  • 260
  • 272
Joe Bailey
  • 27
  • 6
  • 2
    Possible duplicate of [How to read if a checkbox is checked in PHP?](https://stackoverflow.com/questions/4554758/how-to-read-if-a-checkbox-is-checked-in-php) – Martin Jun 15 '18 at 17:10

3 Answers3

0

To understand what you're receiving from the form, var dump it.

var_dump($_POST);

You'll see that when the checkbox is ticked you have the key privacy in the array. So you can use an if condition such as

if (array_key_exists('privacy', $_POST) && !empty($_POST['privacy'])) {
    // send email
}
Alessandro
  • 1,443
  • 9
  • 18
0

just use

if (isset($_POST['subscribe'])){
$subscribe= $_POST['subscribe'];
}

$email_subject = "Joe Bailey Photography Contact Form";
$email_body = "You have received a new message from $fname $lname.\n".
"Here is the message:\n$subject
".
"$fname wishes to subscribe to the Newsletter: $subscribe --\n";
ATechGuy
  • 1,240
  • 8
  • 13
0

Fixed the issue with the following code:

<?php
$response = $_POST["g-recaptcha-response"];
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
    'secret' => '6LeqeRkTAAAAABjyRvL0c1vrqG3ZmV51O0-S3xcz',
    'response' => $_POST["g-recaptcha-response"]
);
$options = array(
    'http' => array (
        'method' => 'POST',
        'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$verify = file_get_contents($url, false, $context);
$captcha_success=json_decode($verify);
if ($captcha_success->success==false) {
    echo "<p>Please go back and verify you are human.</p>";
} else if ($captcha_success->success==true) {

    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $visitor_email = $_POST['Email'];
    $subject = $_POST['subject'];
    $subscribe = $_POST['subscribe'];

    $email_subject = "Joe Bailey Photography Contact Form";
    $email_body = "You have received a new message from $fname $lname.\n".
    "Here is the message:\n$subject
    ".
    "$fname wishes to subscribe to the Newsletter: $subscribe --\n";

    if (isset($_POST['subscribe'])){
        $subscribe= $_POST['subscribe'];
    }

    $to = "enquiries@joebaileyphotography.com";
    $headers = "From: $visitor_email \r\n";
    mail($to,$email_subject,$email_body,$headers);
    //done. redirect to thank-you page.
    header('Location: Contact%20Me%20Thank%20You.html#thankyou');
    function IsInjected($str)
    {
    $injections = array('(\n+)',
          '(\r+)',
          '(\t+)',
          '(%0A+)',
          '(%0D+)',
          '(%08+)',
          '(%09+)'
          );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str))
    {
        return true;
    }
        else
    {
        return false;
    }
    }
}
?> 

Outputs an email as follows: You have received a new message from Joe Bailey. Here is the message: test Joe wishes to subscribe to the Newsletter: Yes -- OR You have received a new message from Joe Bailey. Here is the message: test Joe wishes to subscribe to the Newsletter: --

Also changed the value of HTML element #subscribeNews to "Yes"

Messy, but works.

Joe Bailey
  • 27
  • 6