Jump to content

Emailing Multiple Checkbox Values


mtippery

Recommended Posts

Hi. I have created an HTML form that includes a few checkbox groups that I would like the visitors to be able to check all values that apply to them. I have searched online and have tried figuring out a way to have multiple values handled and emailed to my account. I do not know that much about PHP so everything that I have read online makes it seem like it would be easy enough to do but I do not know how or where to apply it into my code.  Can someone PLEASE help me. Thanks so much in advance.

 

Here is what I have so far:

 

(HTML form - I have shortened it)

 

<form action="volunteer-form2.php" method="post">

<table width="595" class="style8">

  <tr>

    <td colspan="3">Your Interested In Volunteering For: (check all that apply)</td>

  </tr>

  <tr>

    <td width="177" height="30"><input type="checkbox" name="interested[]" value="Home Repairs" /> Home Repairs</td>

    <td width="177"><input type="checkbox" name="interested[]" value="Yard Maintenance" /> Yard Maintenance</td>

    <td width="219"><input type="checkbox" name="interested[]" value="Gardening" /> Gardening</td>

    </tr>

  <tr>

    <td height="30"><input type="checkbox" name="interested[]" value="Fund Raising" /> Fund Raising</td>

    <td><input type="checkbox" name="interested[]" value="Special Events" /> Special Events</td>

    <td><input type="checkbox" name="interested[]" value="Public Relations" /> Public Relations</td>

    </tr>

  <tr>

    <td height="30"><input type="checkbox" name="interested[]" value="Driving" /> Driving</td>

    <td><input type="checkbox" name="interested[]" value="Mentoring Children" /> Mentoring Children</td>

    <td><input type="checkbox" name="interested[]" value="Special Programs For Families" /> Special Programs For Families</td>

    </tr>

  <tr>

    <td height="30" colspan="3"><input type="checkbox" name="interested[]" value="Organizing Supplies" /> Organizing Supplies In Our Warehouse Facilities</td>

  </tr>

  <tr>

    <td height="10" colspan="3"> </td>

  </tr>

</table>

<input type="submit" name="submit" value="Submit" />

</form>

 

 

(PHP Code)

 

<?php 

 

$to = "[email protected]" ; 

$from = $_REQUEST['email'] ; 

$name = $_REQUEST['firstname'] ; 

$headers = "From: $from"; 

$subject = "Web Inquiry";

 

$fields = array();

$fields{"firstname"} = "Name"; 

$fields{"lastname"} = "Last Name"; 

$fields{"street"} = "Address"; 

$fields{"city"} = "City"; 

$fields{"state"} = "State"; 

$fields{"zip"} = "Zip";

$fields{"phone"} = "Phone Number"; 

$fields{"email"} = "Email Address";

$fields{"contactpreference"} = "Contact Preference";

$fields{"contacttime"} = "Best Time To Contact";

$fields{"email"} = "Email Address";

$fields{"learn"} = "Learned About The Program";

$fields{"interested"} = "Interested In";

$fields{"available"} = "Available";

$fields{"medicalconditions"} = "Medical Conditions";

$fields{"emergencycontact"} = "Emergency Contact";

$fields{"contactphone"} = "Emerency Contact Phone";

$fields{"contactemail"} = "Emergency Contact Email";

$fields{"comments"} = "Question or Comment";

 

$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 

 

$headers2 = "From: [email protected]"; 

$subject2 = "Thank you for contacting us"; 

$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible. If you have any more questions, please visit our website at www.mysitename.com"; 

 

 

if($from == '') {print "You have not entered an email, please go back and try again";} 

else { 

$send = mail($to, $subject, $body, $headers); 

$send2 = mail($from, $subject2, $autoreply, $headers2); 

if($send)  {header( "Location: thankyou.html" );} 

else 

{print "We encountered an error sending your mail, please notify our webmaster at [email protected]"; } 

}}

 

?>

Link to comment
https://forums.phpfreaks.com/topic/198149-emailing-multiple-checkbox-values/
Share on other sites

PHP will pickup the checkbox values as an array called 'interested' because you used interested[] as the form field names. So you just need to loop through the $_POST['interested'] array and add the values to the email body.

 

$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){    $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }  

// now put the interested checkboxes into the email
$body .= "Interested in:\n";
if(isset($_POST['interested']) && is_array($_POST['interested'])
{
    foreach($_POST['interested'] as $value)
    {
    $body .= $value . "\n";
    }
}

I am sorry, but I couldn't get it to work.  I kept getting syntax errors even though I copied and pasted it into my code.  :shrug:

 

From reading through other forumns, I was able to get it to where I received all of the checked results for "interested" but what if I have other checkbox groups I would like to have the results show as well?  How would I modify the php script I have now to allow for my other groups?  (I hope that makes sense).  Here is what I have again.  PLEASE HELP!!!!

 

<?php  

$to = "[email protected]" ;  
$from = $_REQUEST['email'] ;  
$name = $_REQUEST['firstname'] ;  
$headers = "From: $from";  
$subject = "Web Inquiry";

$fields = array(); 
$fields{"firstname"} = "Name";  
$fields{"lastname"} = "Last Name";  
$fields{"street"} = "Address";  
$fields{"city"} = "City";  
$fields{"state"} = "State";  
$fields{"zip"} = "Zip"; 
$fields{"phone"} = "Phone Number";  
$fields{"email"} = "Email Address";
$fields{"contactpreference"} = "Contact Preference";
$fields{"contacttime"} = "Best Time To Contact";
$fields{"email"} = "Email Address";
$fields{"learn"} = "Learned About The Program";
$fields{"interested"} = "Interested In";
$fields{"available"} = "Available";
$fields{"medicalconditions"} = "Medical Conditions";
$fields{"emergencycontact"} = "Emergency Contact"; 
$fields{"contactphone"} = "Emerency Contact Phone"; 
$fields{"contactemail"} = "Emergency Contact Email"; 
$fields{"comments"} = "Question or Comment";

$body = "We have received the following information:\n\n"; 
foreach($fields as $a => $b) 
{ 
if($a == 'interested' && is_array($_REQUEST[$a])) 
{ 
$body .= sprintf("%20s:\n",$b); 
foreach($_REQUEST[$a] as $interested) 
{ 
$body .= "> $interested\n"; 
} 
} 
else 
{ 
$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); 
} 
} 


$headers2 = "From: [email protected]";  
$subject2 = "Thank you for contacting us";  
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible. If you have any more questions, please visit our website at www.mywebsite.com";  


if($from == '') {print "You have not entered an email, please go back and try again";}  
else {  
$send = mail($to, $subject, $body, $headers);  
$send2 = mail($from, $subject2, $autoreply, $headers2);  
if($send)  {header( "Location: thankyou.html" );}  
else  
{print "We encountered an error sending your mail, please notify our webmaster at [email protected]"; }  
}

?> 

 

(MY HTML FORM)

 

<form action="volunteer-form2.php" method="post">

<table width="595" class="style8">

  <tr>

    <td width="107" height="30">First Name*</td>

    <td width="472" height="30"><span id="sprytextfield1">

      <input name="firstname" type="text" size="30" />

      <span class="textfieldRequiredMsg">A value is required.</span></span></td>

  </tr>

  <tr>

    <td height="30">Last Name*</td>

    <td><span id="sprytextfield2">

      <input name="lastname" type="text" size="30" />

      <span class="textfieldRequiredMsg">A value is required.</span></span></td>

  </tr>

  <tr>

    <td height="30">Address*</td>

    <td><span id="sprytextfield3">

      <input name="street" type="text" size="30" />

      <span class="textfieldRequiredMsg">A value is required.</span></span></td>

  </tr>

  <tr>

    <td height="30">City*</td>

    <td><span id="sprytextfield4">

      <input name="city" type="text" size="30" />

      <span class="textfieldRequiredMsg">A value is required.</span></span></td>

  </tr>

  <tr>

    <td>State*</td>

    <td>

      <select name="state">

<option value="" selected="selected">Please Choose</option>

<option value="AL">Alabama</option>

<option value="AK">Alaska</option>

<option value="AZ">Arizona</option>

<option value="AR">Arkansas</option>

<option value="CA">California</option>

<option value="CO">Colorado</option>

<option value="CT">Connecticut</option>

<option value="DE">Delaware</option>

<option value="FL">Florida</option>

<option value="GA">Georgia</option>

<option value="HI">Hawaii</option>

<option value="ID">Idaho</option>

<option value="IL">Illinois</option>

<option value="IN">Indiana</option>

<option value="IA">Iowa</option>

<option value="KS">Kansas</option>

<option value="KY">Kentucky</option>

<option value="LA">Louisiana</option>

<option value="ME">Maine</option>

<option value="MD">Maryland</option>

<option value="MA">Massachusetts</option>

<option value="MI">Michigan</option>

<option value="MN">Minnesota</option>

<option value="MS">Mississippi</option>

<option value="MO">Missouri</option>

<option value="MT">Montana</option>

<option value="NC">North Carolina</option>

<option value="ND">North Dakota</option>

<option value="NE">Nebraska</option>

<option value="NV">Nevada</option>

<option value="NH">New Hampshire</option>

<option value="NJ">New Jersey</option>

<option value="NM">New Mexico</option>

<option value="NY">New York</option>

<option value="OH">Ohio</option>

<option value="OK">Oklahoma</option>

<option value="OR">Oregon</option>

<option value="PA">Pennsylvania</option>

<option value="PR">Puerto Rico</option>

<option value="RI">Rhode Island</option>

<option value="SC">South Carolina</option>

<option value="SD">South Dakota</option>

<option value="TN">Tennessee</option>

<option value="TX">Texas</option>

<option value="UT">Utah</option>

<option value="VT">Vermont</option>

<option value="VA">Virginia</option>

<option value="WA">Washington</option>

<option value="DC">Washington, D.C</option>

<option value="WV">West Virginia</option>

<option value="WI">Wisconsin</option>

<option value="WY">Wyoming</option>

    </select>    </td>

  </tr>

  <tr>

    <td height="30">Zip*</td>

    <td><span id="sprytextfield5">

    <input name="zip" type="text" size="30" />

    <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>

  </tr>

  <tr>

  <td height="30">Phone Number*</td>

    <td><span id="sprytextfield6">

    <input name="phone" type="text" size="30" />

    <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>

  </tr>

  <tr>

    <td height="30">Email*</td>

    <td><span id="sprytextfield7">

    <input name="email" type="text" size="30" />

    <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>

  </tr>

  <tr>

    <td height="10" colspan="2"> </td>

  </tr>

</table><br />

 

<table width="595" class="style8">

  <tr>

    <td width="241" height="30">You Prefer To Be Contacted By:</td>

    <td width="110"><input type="checkbox" name="contactpreference[]" value="Telephone" /> Telephone</td>

    <td width="110"><input type="checkbox" name="contactpreference[]" value="Email" /> Email</td>

    <td width="106"> </td>

  </tr>

  <tr>

    <td height="30">Best Time To Contact You:</td>

    <td><input type="checkbox" name="contacttime[]" value="Morning"/> Morning</td>

    <td><input type="checkbox" name="contacttime[]" value="Afternoon" /> Afternoon</td>

    <td><input type="checkbox" name="contacttime[]" value="Evening" /> Evening</td>

  </tr>

  <tr>

    <td>How Did You Learn About This Program:</td>

    <td colspan="3"><textarea name="learn" cols="35" rows="2"></textarea></td>

  </tr>

  <tr>

    <td height="10" colspan="3"> </td>

  </tr>

</table>

<br />

<table width="595" class="style8">

  <tr>

    <td colspan="3">Your Interested In Volunteering For: (check all that apply)</td>

  </tr>

  <tr>

    <td width="177" height="30"><input type="checkbox" name="interested[]" value="Home Repairs" /> Home Repairs</td>

    <td width="177"><input type="checkbox" name="interested[]" value="Yard Maintenance" /> Yard Maintenance</td>

    <td width="219"><input type="checkbox" name="interested[]" value="Gardening" /> Gardening</td>

    </tr>

  <tr>

    <td height="30"><input type="checkbox" name="interested[]" value="Fund Raising" /> Fund Raising</td>

    <td><input type="checkbox" name="interested[]" value="Special Events" /> Special Events</td>

    <td><input type="checkbox" name="interested[]" value="Public Relations" /> Public Relations</td>

    </tr>

  <tr>

    <td height="30"><input type="checkbox" name="interested[]" value="Driving" /> Driving</td>

    <td><input type="checkbox" name="interested[]" value="Mentoring Children" /> Mentoring Children</td>

    <td><input type="checkbox" name="interested[]" value="Special Programs For Families" /> Special Programs For Families</td>

    </tr>

  <tr>

    <td height="30" colspan="3"><input type="checkbox" name="interested[]" value="Organizing Supplies" /> Organizing Supplies In Our Warehouse Facilities</td>

  </tr>

  <tr>

    <td height="10" colspan="3"> </td>

  </tr>

</table><br />

<table width="595" border="0">

  <tr>

    <td colspan="3">Available To Volunteer: (check all that apply)</td>

  </tr>

  <tr>

    <td width="174" height="30"><input type="checkbox" name="available[]" value="Days" /> Days</td>

    <td width="175"><input type="checkbox" name="available[]" value="Weekdays" /> Weekdays</td>

    <td width="224"><input type="checkbox" name="available[]" value="Long Term" /> Long Term</td>

  </tr>

  <tr>

    <td height="30"><input type="checkbox" name="available[]" value="Evenings" /> Evenings</td>

    <td><input type="checkbox" name="available[]" value="Weekends" /> Weekends</td>

    <td> </td>

  </tr>

  <tr>

    <td height="10" colspan="3"> </td>

  </tr>

</table><br />

<table width="595" class="style8">

  <tr>

    <td width="206" height="50">Special Medical Conditions That Might Affect Your Volunteering:</td>

    <td colspan="2"><textarea name="medicalconditions" cols="35" rows="2" ></textarea></td>

  </tr>

  <tr>

    <td height="30">Emergency Contact Name</td>

    <td colspan="2"><input name="emergencycontact" type="text" size="45" /></td>

  </tr>

  <tr>

    <td height="30">Emergency Contact Phone</td>

    <td width="178"><span id="sprytextfield8">

      <input name="emergencyphone1" type="text" size="18" />

    </span>

      Day</td>

    <td width="197"><span id="sprytextfield9">

      <input name="emergencyphone2" type="text" size="18" />

    </span>

      Evening</td>

  </tr>

  <tr>

    <td height="10" colspan="3"> </td>

  </tr>

</table><br />

<table width="595" class="style8">

  <tr>

    <td width="178" height="30">Organization Name:</td>

    <td colspan="3"><input type="text" name="organizationname" size="40" /></td>

  </tr>

  <tr>

    <td height="30">Organization Type:</td>

    <td width="111"><input type="checkbox" name="type[]" value="School" /> School</td>

    <td width="115"><input type="checkbox" name="type[]" value="Company" /> Company</td>

    <td width="163"><input type="checkbox" name="type[]" value="Other Organization" /> Other Organization</td>

  </tr>

  <tr>

    <td height="30">Organization Contact Name:</td>

    <td colspan="3"><input name="contactname" type="text" size="40" /></td>

  </tr>

  <tr>

    <td height="30">Organization Contact Phone:</td>

<td colspan="3"><span id="sprytextfield10">

  <input name="contactphone" type="text" size="40" />

  <span class="textfieldInvalidFormatMsg">Invalid format.</span> </span></td>

  </tr>

  <tr>

    <td height="30">Organization Contact Email:</td>

    <td colspan="3"><span id="sprytextfield11">

      <input name="contactemail" type="text" size="40" />

      <span class="textfieldInvalidFormatMsg">Invalid format.</span>      </span></td>

  </tr>

  <tr>

    <td colspan="4" height="10"> </td>

</tr>

</table><br />

<table width="595" class="style8">

  <tr>

    <td width="161" height="50">Questions Or Comments:</td>

    <td width="418"><textarea name="comments" cols="35" rows="2" ></textarea></td>

  </tr>

  <tr>

    <td height="10" colspan="2"> </td>

  </tr>

</table><br />

<span class="style4">Shelter Outreach Plus is a 501©(03) public charity. All contributions to Shelter Outreach Plus are tax deductible to the extent provided by law. Thank you!</span>

<br /><br /><br />

<center><input type="submit" name="submit" value="Submit" />

    <input type="reset" name="reset" value="Reset" />

</center>

</form>

The syntax error would have been because I missed a ) character. It should be

 

if(isset($_POST['interested']) && is_array($_POST['interested']))

 

To get other checkbox groups just remove the $a == 'interested' part of your code so it is just

 

if(is_array($_REQUEST[$a]))

THANK YOU, THANK YOU, THANK YOU, THANK YOU.... and in case I didn't tell you.... THANK YOU!!!!!!!!

 

It works.  I have been struggling with this for two days now.  Yaye!!!!!!!

 

I am definitely going to invest in a few more PHP books.  I took some webdesign classes through the Art Institute but I feel like they kinda left me hanging.  Thank you so much.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.