PHP email with Checkbox

Having issues getting my very simple email form to work. What I want to happen is the email will only send if the form checkbox has been selected.

HTML CODE:

<form class="form-inline contact_box" action="contact.php" method="post">
                        <input name="cf_name" class="form-control input_box" type"text" placeholder="Full Name" required>
                        <input name="cf_email" class="form-control input_box" type"text"  placeholder="Email" required>
                        <input name="cf_subject" class="form-control input_box" type"text"  placeholder="Subject" required>
                        <textarea name="cf_message" class="form-control input_box" rows="20" placeholder="Message" required></textarea>
                        Are you human?
                        <input type="checkbox" name="question" value="Yes" />
      					&nbsp;<br/>
                        &nbsp;<br/>
                        <button type="submit" class="btn btn-default">Send Message</button>
                    </form>

PHP CODE:

<?php
if(isset($_POST['Yes'])){

$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_subject = $_POST['cf_subject'];
$field_message = $_POST['cf_message'];

$mail_to = '[email protected]';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Subject: '.$field_subject."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
	<script language="javascript" type="text/javascript">
		alert('Thank you for the message. We will contact you shortly.');
		window.location = 'contact.html';
	</script>
<?php
}
else { ?>
	<script language="javascript" type="text/javascript">
		alert('Message failed. Please, send an email to [email protected]');
		window.location = 'contact.html';
	</script>
<?php
}}
?>

Any help would be much appreciated.

And that would be due to this decision statement.

Any suggestions on how to correct it?

Change it to,

if($_SERVER['REQUEST_METHOD'] == 'POST') {

Thank you for your help. Just out of interest, how is that line of code linked to the forms checkbox?

try the key (name=):

if (isset($_POST['question'])) {

It isn’t, that’s why it works. If the form is submitted via post, it will process the request, how it get’s there is irrelevant.

Sponsor our Newsletter | Privacy Policy | Terms of Service