cherch
cherch

Reputation: 27

How to add checkbox results from html5 form?

I'm using a super super simple form in an html5 document. I do not need validators or any fancy stuff at the moment but I'm having trouble finding out how to get the result of a checkbox to be included in the email.

The form code in the html is:

<div id="subscribe_form">
<form action="mail.php" method="post" target="_blank">

 <input type="text" name="name" placeholder="Name" /><br>

<input type="email" name="email" placeholder="Email" /><br>

<textarea name="message" rows="6" cols="30" placeholder="Mailing Address" ></textarea><br />


<input type="checkbox" id="hascurrent" value="yes" />
<label for="hascurrent">I've already received the current issue of Critters &amp; Gods.</label>

<br>
<button type="submit" value="Send">Subscribe</button>

</form>

And the entire mail.php form includes:

<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="Subscriber: $name Email: $email Address: $message Has current issue: $hascurrent";
$recipient = "[email protected]";
$subject = "New Subscription";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "<div id='form_echo'> Thank you, $name, for subscribing to Critters & Gods. Your subscription is completely free and will not expire. If at any time you wish to unsubscribe from Critters & Gods, visit the about page for information. </div>";
?>

Do I have the checkbox formatted correctly in html5 (it shows up fine but is my coding correct?) and exactly what do I put in the php file to have it sent with the email? Thank you!!!

Upvotes: 0

Views: 2388

Answers (1)

Nikhil Patel
Nikhil Patel

Reputation: 1781

Add a name attribute to your checkbox input :

<input type="checkbox" id="hascurrent" name="hasCurrent" value="yes" />

Then in you PHP, just read it as other variables :

$hasCurrent = $_POST['hasCurrent'];

The value of $hasCurrent will be yes if checked, and blank otherwise.

Upvotes: 2

Related Questions