Need to know how to inlcude a checkbox in the form handler

I have a form that will have the visitor either check or not check a box. If the box is checked I need the email to show that it is checked. How do I set this up? Here is the code.


<?php
$errors = '';
$myemail = 'corifoxworthy@foxdenwebsolutions.com';
if(empty($_POST['name'])  ||
   empty($_POST['email']))
{
    $errors .= "\\r\
 Error: all fields are required";
}

$name = $_POST['name'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$memory = $_POST['memory'];
$honor = $_POST['honor'];


if (!eregi(
"^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3})$",
$email))
{
    $errors .= "\\r\
 Error: Invalid email address";
}
if( empty($errors))
{
	$to = $myemail;
	$email_subject = "Wings of Hope for Pancreatic Cancer Research Dondation: $name";
	$email_body = "You have received a new donation. ".
	" Here are the details:\\r\
 Name: $name\\r\
 Address: $address\\r\
 City: $city\\r\
 State: $state\\r\
 Zip Code: $zipcode\\r\
 Email Address: $email\\r\
 phone: $phone\\r\
 memory: $memory\\r\
 honor: $honor";
	
	 $headers =    "MIME-Versin: 1.0\\r\
" .
               "Content-type: text/plain; charset=ISO-8859-1; format=flowed\\r\
" .
               "Content-Transfer-Encoding: 8bit\\r\
" .
               "From: $myemail\\r\
" .
                "Reply: $email_address\\r\
".
               "Return-Path: $email_address\\r\
".
               "X-Mailer: PHP" . phpversion();


	
	mail($to,$email_subject,$email_body,$headers);
	//redirect to the 'thank you' page
header('Location: thank_you.html');
exit();
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>

Here is the HTML


<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>WINGS OF HOPE FOR PANCREATIC CANCER RESEARCH - DONATE</title>
<link href="css/wings.css" rel="stylesheet" type="text/css">
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>

<body>
<div class="outer">
<header>
  <h1>Wings of Hope for Pancreatic Cancer Research</h1>
</header>
<div class="nav_container">
  <ul>
    <li><a href="index.html">Home</a></li>
    <li><a href="mission.html">Mission</a></li>
    <li><a href="research.html">Ongoing<br>
      Research</a></li>
    <li><a href="directors.html">Board of<br>
      Directors</a></li>
    <li><a href="events.html">Events</a></li>
    <li><a href="news.html">News</a></li>
    <li><a href="comments.html">Patient/Caregiver<br>
      Comments</a></li>
    <li class="active"><a href="#">Donate</a></li>
  </ul>
</div>
<div class="content_container">
  <section class="row">
    <div class="sec_one">
      <p>Your donation directly helps fund the much needed research to end this deadly disease. You may designate your donation (below) in memory or honor of a loved one, friend, caregiver, etc.</p>
      <p>After you have filled out the section below,  please click the DONATION button and you will be taken to PayPal to donate any amount you choose. If you do not have a PayPal account, they are free, secure and easy to set up, or you may donate via check. </p>
      <p>Our heartfelt thank you for helping in this cause.</p>
    </div>
    <div>
      <p><img src="images/gulls.png" alt="gulls"></p>
    </div>
  </section>
  <div class="hline"></div>
  <section class="row">
    <div class="sec_one">
      <form action="contact-form-handler.php" method="post" id="contactform">
        <br>
        <p>
          <label for='name'>Name:</label>
        </p>
        <p>
          <input type="text" name="name" size="50" maxlength="100" />
        </p>
        <p>
          <label for='address'>Address:</label>
        </p>
        <p>
          <input type="text" name="address" size="50" maxlength="100" />
        </p>
        <p>
          <label for='city'>City:</label>
        </p>
        <p>
          <input type="text" name="city" size="25" maxlength="100" />
        </p>
        <p>
          <label for='state'>State:</label>
        </p>
        <p>
          <input type="text" name="state" size="25" maxlength="100" />
        </p>
        <p>
          <label for='zipcode'>Zip Code:</label>
        </p>
        <p>
          <input type="text" name="zipcode" size="15" maxlength="10" />
        </p>
        <p>
          <label for='email'>Email:</label>
        </p>
        <p>
          <input type="text" name="email" size="50" maxlength="100" />
        </p>
        <p>
          <label for='phone'>Phone (optional):</label>
        </p>
        <p>
          <input type="text" name="phone" size="25" maxlength="100" />
        </p>
        <p>
          <label for='memory'>I would like to make my donation in memory of:</label>
        </p>
        <p>
          <input type="text" name="memory"  size="50" maxlength="100" />
        </p>
        <p>or</p>
        <p>
          <label for='honor'>I would like to make my donation in honor of:</label>
        </p>
        <p>
          <input type="text" name="honor" size="50" maxlength="100" />
        </p>
        <p>
          <label for='checkbox'>Please check box if you would like the family of the person your donation is honoring to receive written notification of your donation:</label>
        </p>
        <p>
          <input type="checkbox" name="checkbox" />
        </p>
        <p>If you prefer to make your donation by check, please make check to: <br>
          WINGS OF HOPE FOR PANCREATIC CANCER RESEARCH and send to: <br>
        <p>Maureen A Shul</p>
        <p>Wings of Hope for Pancreatic Cancer Research</p>
        <p>8404 Brambleridge Drive</p>
        <p>Castle Pines, Colorado 80108</p>
        <p>
          <input type="submit" value="Submit" />
          &nbsp;
          <input type="reset" value="Reset" />
        </p>
      </form>
    </div>
    <div>
      <p><img src="images/donate_page.png" alt="tulips"></p>
    </div>
  </section>
  <div class="hline"></div>
  <section class="row">
    <div class="sec_one">
      <p class="behope"><strong>JOIN THE EFFORT…BE A PART OF THE POSSIBLE, BE THE HOPE.</strong></p>
      <div class="form">
        <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
          <input type="hidden" name="cmd" value="_s-xclick">
          <input type="hidden" name="hosted_button_id" value="PEZ2ECYRKA42A">
          <input type="image" src="http://wingsofhopepcr.org/images/donate_button.png"  name="submit" alt="PayPal - The safer, easier way to pay online!">
          <img alt="" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
        </form>
      </div>
    </div>
  </section>
  <!-- content_container -->
  <div class="footer_container">
    <div class="row">
      <ul class="left">
        <li><a href="index.html">Home</a></li>
        <li><a href="mission.html">Mission</a></li>
        <li><a href="research.html">Ongoing Research</a></li>
      </ul>
      <ul class="middle">
        <li><a href="directors.html">Board of Directors</a></li>
        <li><a href="events.html">Events</a></li>
        <li><a href="news.html">News</a></li>
        <li><a href="comments.html">Patient/Caregiver Comments</a></li>
      </ul>
      <ul class="right">
        <li><a href="sponsors.html">Sponsors</a></li>
        <li><a href="donate.html">Donate/Volunteer</a></li>
        <li><a href="contact.html">Contact</a></li>
      </ul>
    </div>
    <div class="row">
      <div class="fb_link"><a href="https://www.facebook.com/WingsOfHopePCR?fref=ts" target="_blank"><img src="images/facebook_link.jpg" alt="facebook Logo Image"></a></div>
      <div class="twitter_link"><a href="https://twitter.com/WingsofHopePCR" target="_blank"><img src="images/twitter_link.jpg" alt="Twitter Logo Image"></a></div>
    </div>
    <div class="copyright">Copyright &copy; 2012 Wings of Hope for Pancreatic Cancer Research</div>
    <div class="designBy"> Website Design By <a href="http://foxdenwebsolutions.com" target="_blank">Foxden Web Solutions</a> </div>
  </div>
  <!-- .footer_container -->
</div>
<!-- .outer -->
</body>
</html>

I find that the simplest way to handle checkboxes is to provide two values in the HTML - one for when the checkbox is checked and one for when it isn’t. That way one of the two values is always passed to the server and you can easily identify whether it is checked or not from the value.

<input type="hidden" name="x" value="no">
<input type="checkbox" name="x" value="yes">

That sets $_POST[‘x’] to ‘yes’ if it is checked and ‘no’ if it isn’t.

Thanks felgall. I am not sure exactly how to set up the form handler page thought.

$checkbox = $_POST[‘x’];

Do I put yes where x is or what?

‘x’ is whatever name you give to the checkbox field in the HTML

The checkbox is not working. Here is what I added to HTML

<p>
<input type=“hidden” name=“checkbox” value=“no”>
<input type=“checkbox” name=“checkbox” value=“yes”>
</p>

Here is what is in the form handler for the body of the email

$name = $_POST[‘name’];
$address = $_POST[‘address’];
$city = $_POST[‘city’];
$state = $_POST[‘state’];
$zipcode = $_POST[‘zipcode’];
$email = $_POST[‘email’];
$phone = $_POST[‘phone’];
$memory = $_POST[‘in memory’];
$honor = $_POST[‘honor’];
$checkbox = $_POST[‘checkbox’];

if (!eregi(
[1]+(\.[_a-z0-9-]+)@[a-z0-9-]+(\.[a-z0-9-]+)(\.[a-z]{2,3})$”,
$email))
{
$errors .= “\r
Error: Invalid email address”;
}
if( empty($errors))
{
$to = $myemail;
$email_subject = “Wings of Hope for Pancreatic Cancer Research Donation: $name”;
$email_body = "You have received a new donation. “.
" Here are the details:\r
Name: $name\r
Address: $address\r
City: $city\r
State: $state\r
Zip Code: $zipcode\r
Email Address: $email\r
phone: $phone\r
memory: $memory\r
honor: $honor\r
Checkbox: $checkbox”;

Also noticed that the name in the in memory or in honor is not showing up now either so here is the full form


<form action="contact-form-handler.php" method="post" id="contactform">
        <br>
        <p>
          <label for='name'>Name:</label>
        </p>
        <p>
          <input type="text" name="name" size="50" maxlength="100" />
        </p>
        <p>
          <label for='address'>Address:</label>
        </p>
        <p>
          <input type="text" name="address" size="50" maxlength="100" />
        </p>
        <p>
          <label for='city'>City:</label>
        </p>
        <p>
          <input type="text" name="city" size="25" maxlength="100" />
        </p>
        <p>
          <label for='state'>State:</label>
        </p>
        <p>
          <input type="text" name="state" size="25" maxlength="100" />
        </p>
        <p>
          <label for='zipcode'>Zip Code:</label>
        </p>
        <p>
          <input type="text" name="zipcode" size="15" maxlength="10" />
        </p>
        <p>
          <label for='email'>Email:</label>
        </p>
        <p>
          <input type="text" name="email" size="50" maxlength="100" />
        </p>
        <p>
          <label for='phone'>Phone (optional):</label>
        </p>
        <p>
          <input type="text" name="phone" size="25" maxlength="100" />
        </p>
        <p>
          <label for='in memory'>I would like to make my donation in memory of:</label>
        </p>
        <p>
          <input type="text" name="in memory"  size="50" maxlength="100" />
        </p>
        <p>or</p>
        <p>
          <label for='in honor'>I would like to make my donation in honor of:</label>
        </p>
        <p>
          <input type="text" name="in honor" size="50" maxlength="100" />
        </p>
        <p>
          <label for='checkbox'>Please check the box if you would like the family of the person your donation is honoring to receive written notification of your donation:</label>
        </p>
        <p>
          <input type="hidden" name="checkbox" value="no">
		  <input type="checkbox" name="checkbox" value="yes">
        </p>
        <p>If you prefer to make your donation by check, please make check to: <br>
          WINGS OF HOPE FOR PANCREATIC CANCER RESEARCH and send to: <br>
        <p>Maureen A Shul</p>
        <p>Wings of Hope for Pancreatic Cancer Research</p>
        <p>8404 Brambleridge Drive</p>
        <p>Castle Pines, Colorado 80108</p>
        <p>
          <input type="submit" value="Submit" />
          &nbsp;
          <input type="reset" value="Reset" />
        </p>
      </form>


  1. _a-z0-9- ↩︎

Ok I played around and now have the checkbox showing yes or no but the name in the memory or honor field is not showing up.


<form action="contact-form-handler.php" method="post" id="contactform">
        <br>
        <p>
          <label for='name'>Name:</label>
        </p>
        <p>
          <input type="text" name="name" size="50" maxlength="100" />
        </p>
        <p>
          <label for='address'>Address:</label>
        </p>
        <p>
          <input type="text" name="address" size="50" maxlength="100" />
        </p>
        <p>
          <label for='city'>City:</label>
        </p>
        <p>
          <input type="text" name="city" size="25" maxlength="100" />
        </p>
        <p>
          <label for='state'>State:</label>
        </p>
        <p>
          <input type="text" name="state" size="25" maxlength="100" />
        </p>
        <p>
          <label for='zipcode'>Zip Code:</label>
        </p>
        <p>
          <input type="text" name="zipcode" size="15" maxlength="10" />
        </p>
        <p>
          <label for='email'>Email:</label>
        </p>
        <p>
          <input type="text" name="email" size="50" maxlength="100" />
        </p>
        <p>
          <label for='phone'>Phone (optional):</label>
        </p>
        <p>
          <input type="text" name="phone" size="25" maxlength="100" />
        </p>
        <p>
          <label for='memory'>I would like to make my donation in memory of:</label>
        </p>
        <p>
          <input type="text" name="memory"  size="50" maxlength="100" />
        </p>
        <p>or</p>
        <p>
          <label for='honor'>I would like to make my donation in honor of:</label>
        </p>
        <p>
          <input type="text" name="honor" size="50" maxlength="100" />
        </p>
        <p>
          <label for='checkbox'>Please check the box if you would like the family of the person your donation is honoring to receive written notification of your donation:</label>
        </p>
        <p>
          <input type="hidden" name="checkbox" value="no">
		  <input type="checkbox" name="checkbox" value="yes">
        </p>
</form>

handler


<?php 
$errors = '';
$myemail = 'corifoxworthy@foxdenwebsolutions.com';
if(empty($_POST['name'])  || 
   empty($_POST['email'])) 
{
    $errors .= "\\r\
 Error: all fields are required";
}

$name = $_POST['name']; 
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$memory = $_POST['in memory'];
$honor = $_POST['in honor'];
$checkbox = $_POST['checkbox'];


if (!eregi(
"^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3})$", 
$email))
{
    $errors .= "\\r\
 Error: Invalid email address";
}
if( empty($errors))
{
	$to = $myemail; 
	$email_subject = "Wings of Hope for Pancreatic Cancer Research Donation: $name";
	$email_body = "You have received a new donation. ".
	" Here are the details:\\r\
 Name: $name\\r\
 Address: $address\\r\
 City: $city\\r\
 State: $state\\r\
 Zip Code: $zipcode\\r\
 Email Address: $email\\r\
 phone: $phone\\r\
 in memory: $memory\\r\
 in honor: $honor\\r\
 Checkbox: $checkbox"; 
	
	 $headers =    "MIME-Versin: 1.0\\r\
" . 
               "Content-type: text/plain; charset=ISO-8859-1; format=flowed\\r\
" . 
               "Content-Transfer-Encoding: 8bit\\r\
" . 
               "From: $myemail\\r\
" . 
                "Reply: $email_address\\r\
".
               "Return-Path: $email_address\\r\
".
               "X-Mailer: PHP" . phpversion();  


	
	mail($to,$email_subject,$email_body,$headers);
	//redirect to the 'thank you' page
header('Location: thank_you.html');
exit();
} 
?>

<!doctype html>
<html>
<head>
	<title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>