Page 1 of 1
Emailing check box values from a form
Posted: Wed May 23, 2012 10:57 am
by phpsounds
I've been struggling to get this right. I'm just trying to have the check box values included in the email that is sent when submitting the form. I get that I have to loop through the field, which is Activities[] in the html part of the form. But something's not right. If someone could point out where I'm going wrong, I'd be very grateful. Thanks
Code: Select all
<?php
$to = $_REQUEST['SendTo'] ;
$from = $_REQUEST['Email'] ;
$namefirst = $_REQUEST['NameFirst'] ;
$namelast = $_REQUEST['NameLast'] ;
$headers = "From: $from";
$subject = "Email subject";
foreach($_REQUEST['Activities'] as $requestedactivities) {
$check_msg .= "Checked: $requestedactivities\n";
}
$fields = array();
$fields["NameFirst"] = "First Name";
$fields["NameLast"] = "Last Name";
$fields["Address1"] = "Address 1";
$fields["Address2"] = "Address 2";
$fields["City"] = "City";
$fields["County"] = "County";
$fields["Zip"] = "Zip";
$fields["Country"] = "Country";
$fields["Email"] = "Email Address";
$fields["Phone"] = "Phone Number";
$fields["Attending"] = "Attending Ceremony";
$fields["GuestName"] = "Spouse or Guest Name";
$fields["NumberOfChildren"] = "Number of Children";
$fields["TotalInParty"] = "Total Number in Party";
$fields["ArrivalDate"] = "Arrival Date";
$fields["GolfCottage"] = "Interested in Golf Cottage";
$fields["GolfCottageRoomsQty"] = "Number of Golf Cottage Rooms";
$fields["GolfCottagePairings"] = "Golf Cottage Pairings";
$fields["EstuaryCruise"] = "Estuary Cruise";
$fields["Comments"] = "Comments";
$body = "The following information has been submitted via the website:\n\n $check_msg"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n\n",$b,$_REQUEST[$a]); }
$headers2 = "From: email@email.com";
$subject2 = "Thank you for your RSVP";
$autoreply = "We have received your RSVP. \n\n";
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{header( "Location: http://www.test.com/rsvp-submitted.html" );}
else
{print "We encountered an error sending your mail, please notify email@email.com"; }
?>
Re: Emailing check box values from a form
Posted: Wed May 23, 2012 11:00 am
by Celauran
phpsounds wrote:something's not right
Can you elaborate on that?
Re: Emailing check box values from a form
Posted: Wed May 23, 2012 11:22 am
by phpsounds
Yeah sure, sorry. Basically I'm not getting the values that have been checked in the email that's received when the form is submitted. I'm not sure if they are not making it into the variable, or if I'm messing up the placement of the data in the body part of the email.
Re: Emailing check box values from a form
Posted: Wed May 23, 2012 11:47 am
by Celauran
Re: Emailing check box values from a form
Posted: Wed May 23, 2012 11:56 am
by phpsounds
I replaced the foreach loop with your suggestion, hit submit and ended up on a page showing me one value of the check boxes. So, didn't work. There was none of the check box data on the email that was received either.
Re: Emailing check box values from a form
Posted: Wed May 23, 2012 12:35 pm
by Celauran
Can you post the rendered form (ie. the final HTML, not the PHP that generates it)?
Re: Emailing check box values from a form
Posted: Wed May 23, 2012 12:46 pm
by phpsounds
HTML form below. Ignore any fields that might not be in the php, I've removed some to make it simpler.
Code: Select all
<form method="post" action="contact.php">
<input type="hidden" name="SendTo" value="email@email.com" />
<table width="750" border="0" cellspacing="10" cellpadding="0">
<tr>
<td width="180">First Name:</td>
<td width="180"><input type="text" size=25 name="NameFirst" /></td>
<td width="180">Last Name:</td>
<td width="180"><input type="text" size=25 name="NameLast" /></td>
</tr>
<tr>
<td>Address 1:</td>
<td><input type="text" size=25 name="Address1" /></td>
<td>Address 2:</td>
<td><input type="text" size=25 name="Address2" /></td>
</tr>
<tr>
<td>City:</td>
<td><input type="text" size=25 name="City" /></td>
<td>State/County:</td>
<td><input type="text" size=25 name="County" /></td>
</tr>
<tr>
<td>Zip/Postcode:</td>
<td><input type="text" size=25 name="Zip" /></td>
<td>Country:</td>
<td><input type="text" size=25 name="Country" /></td>
</tr>
<tr>
<td>Email Address:</td>
<td><input type="text" size=25 name="Email" /></td>
<td>Phone:</td>
<td><input type="text" size=25 name="Phone" /></td>
</tr>
<tr>
<td colspan="2">Will you be attending the ceremony?</td>
<td><input type="radio" name="Attending" value="Yes" />
Yes</td>
<td><input type="radio" name="Attending" value="No" />
No</td>
</tr>
<tr>
<td colspan="2">Spouse or Guest Name:</td>
<td><input type="text" size=25 name="GuestName" /></td>
<td></td>
</tr>
<tr>
<td colspan="2">Number of children:</td>
<td><input type="text" size=25 name="NumberOfChildren" /></td>
<td></td>
</tr>
<tr>
<td colspan="2">Total number in your party:</td>
<td><input type="text" size=25 name="TotalInParty" /></td>
<td></td>
</tr>
<tr>
<td colspan="2">Estimated date of arrival in the area:</td>
<td><input type="text" size=25 name="ArrivalDate" /></td>
<td></td>
</tr>
<tr>
<td colspan="2">Are you interested in staying in one of the golf cottages?:</td>
<td><input type="radio" name="GolfCottage" value="Yes" />
Yes</td>
<td><input type="radio" name="GolfCottage" value="No" />
No</td>
</tr>
<tr>
<td colspan="2">If yes, number of rooms required:</td>
<td><input type="text" size=25 name="GolfCottageRoomsQty"></td>
<td></td>
</tr>
<tr>
<td colspan="2">Are there others you'd like to be paired with?</td>
<td><input type="text" size=25 name="GolfCottagePairings"></td>
<td></td>
</tr>
<tr>
<td colspan="2">Will you be joining us on our estuary cruise?</td>
<td><input type="radio" name="EstuaryCruise" value="Yes" /> Yes</td>
<td><input type="radio" name="EstuaryCruise" value="No" /> No</td>
</tr>
<tr>
<td colspan="2">Are you interested in any of the following:</td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="2"><input type="checkbox" name="Activities[]" value="Margarita" /> Malice's Margarita Madness</td>
<td colspan="2"><input type="checkbox" name="Activities[]" value="Brunch" /> Saturday A.M. Brunch</td>
</tr>
<tr>
<td colspan="2"><input type="checkbox" name="Activities[]" value="MiniatureGolf" />
Miniature Golf @ Blackbeard's Cove</td>
<td colspan="2"><input type="checkbox" name="Activities[]" value="BirdsOfPrey" />
Birds of Prey Tour & Falconry Exhibition</td>
</tr>
<tr>
<td><input type="checkbox" name="Activities[]" value="Fishing" />
Fishing</td>
<td></td>
<td><input type="checkbox" name="Activities[]" value="BullsBayGolf" />
Golf @ Bulls Bay</td>
<td></td>
</tr>
<tr>
<td>Comments:</td>
<td colspan="2"><textarea name="Comments" rows=5 cols=45></textarea></td>
<td><input type=submit name="send" value="Submit"></td>
</tr>
</table>
</form>
Re: Emailing check box values from a form
Posted: Wed May 23, 2012 1:02 pm
by Celauran
Code: Select all
$check_msg = '';
foreach($_REQUEST['Activities'] as $requestedactivities) {
$check_msg .= "Checked: $requestedactivities\n";
}
Re: Emailing check box values from a form
Posted: Wed May 23, 2012 1:15 pm
by phpsounds
No, didn't work. Still not showing up in the email. If the array is being populated correctly by the foreach loop, is there something wrong with the way the data is being added to the body of the email?
Re: Emailing check box values from a form
Posted: Wed May 23, 2012 1:25 pm
by Celauran
This is working fine for me. (Note I've condensed it into a single page)
Code: Select all
<?php
if (!empty($_POST))
{
$to = $_POST['SendTo'];
$from = $_POST['Email'];
$namefirst = $_POST['NameFirst'];
$namelast = $_POST['NameLast'];
$headers = "From: $from";
$subject = "Email subject";
$check_msg = '';
foreach ($_POST['Activities'] as $requestedactivities)
{
$check_msg .= "Checked: $requestedactivities\n";
}
$fields = array();
$fields["NameFirst"] = "First Name";
$fields["NameLast"] = "Last Name";
$fields["Address1"] = "Address 1";
$fields["Address2"] = "Address 2";
$fields["City"] = "City";
$fields["County"] = "County";
$fields["Zip"] = "Zip";
$fields["Country"] = "Country";
$fields["Email"] = "Email Address";
$fields["Phone"] = "Phone Number";
$fields["Attending"] = "Attending Ceremony";
$fields["GuestName"] = "Spouse or Guest Name";
$fields["NumberOfChildren"] = "Number of Children";
$fields["TotalInParty"] = "Total Number in Party";
$fields["ArrivalDate"] = "Arrival Date";
$fields["GolfCottage"] = "Interested in Golf Cottage";
$fields["GolfCottageRoomsQty"] = "Number of Golf Cottage Rooms";
$fields["GolfCottagePairings"] = "Golf Cottage Pairings";
$fields["EstuaryCruise"] = "Estuary Cruise";
$fields["Comments"] = "Comments";
$body = "The following information has been submitted via the website:\n\n $check_msg";
foreach ($fields as $a => $b)
{
$body .= sprintf("%20s: %s\n\n", $b, $_POST[$a]);
}
echo "<pre>{$body}</pre>";
/*
$headers2 = "From: email@email.com";
$subject2 = "Thank you for your RSVP";
$autoreply = "We have received your RSVP. \n\n";
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if ($send)
{
header("Location: http://www.test.com/rsvp-submitted.html");
}
else
{
print "We encountered an error sending your mail, please notify email@email.com";
}
*/
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body>
<form method="post" action="">
<input type="hidden" name="SendTo" value="email@email.com" />
<table width="750" border="0" cellspacing="10" cellpadding="0">
<tr>
<td width="180">First Name:</td>
<td width="180"><input type="text" size=25 name="NameFirst" /></td>
<td width="180">Last Name:</td>
<td width="180"><input type="text" size=25 name="NameLast" /></td>
</tr>
<tr>
<td>Address 1:</td>
<td><input type="text" size=25 name="Address1" /></td>
<td>Address 2:</td>
<td><input type="text" size=25 name="Address2" /></td>
</tr>
<tr>
<td>City:</td>
<td><input type="text" size=25 name="City" /></td>
<td>State/County:</td>
<td><input type="text" size=25 name="County" /></td>
</tr>
<tr>
<td>Zip/Postcode:</td>
<td><input type="text" size=25 name="Zip" /></td>
<td>Country:</td>
<td><input type="text" size=25 name="Country" /></td>
</tr>
<tr>
<td>Email Address:</td>
<td><input type="text" size=25 name="Email" /></td>
<td>Phone:</td>
<td><input type="text" size=25 name="Phone" /></td>
</tr>
<tr>
<td colspan="2">Will you be attending the ceremony?</td>
<td><input type="radio" name="Attending" value="Yes" />
Yes</td>
<td><input type="radio" name="Attending" value="No" />
No</td>
</tr>
<tr>
<td colspan="2">Spouse or Guest Name:</td>
<td><input type="text" size=25 name="GuestName" /></td>
<td></td>
</tr>
<tr>
<td colspan="2">Number of children:</td>
<td><input type="text" size=25 name="NumberOfChildren" /></td>
<td></td>
</tr>
<tr>
<td colspan="2">Total number in your party:</td>
<td><input type="text" size=25 name="TotalInParty" /></td>
<td></td>
</tr>
<tr>
<td colspan="2">Estimated date of arrival in the area:</td>
<td><input type="text" size=25 name="ArrivalDate" /></td>
<td></td>
</tr>
<tr>
<td colspan="2">Are you interested in staying in one of the golf cottages?:</td>
<td><input type="radio" name="GolfCottage" value="Yes" />
Yes</td>
<td><input type="radio" name="GolfCottage" value="No" />
No</td>
</tr>
<tr>
<td colspan="2">If yes, number of rooms required:</td>
<td><input type="text" size=25 name="GolfCottageRoomsQty"></td>
<td></td>
</tr>
<tr>
<td colspan="2">Are there others you'd like to be paired with?</td>
<td><input type="text" size=25 name="GolfCottagePairings"></td>
<td></td>
</tr>
<tr>
<td colspan="2">Will you be joining us on our estuary cruise?</td>
<td><input type="radio" name="EstuaryCruise" value="Yes" /> Yes</td>
<td><input type="radio" name="EstuaryCruise" value="No" /> No</td>
</tr>
<tr>
<td colspan="2">Are you interested in any of the following:</td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="2"><input type="checkbox" name="Activities[]" value="Margarita" /> Malice's Margarita Madness</td>
<td colspan="2"><input type="checkbox" name="Activities[]" value="Brunch" /> Saturday A.M. Brunch</td>
</tr>
<tr>
<td colspan="2"><input type="checkbox" name="Activities[]" value="MiniatureGolf" />
Miniature Golf @ Blackbeard's Cove</td>
<td colspan="2"><input type="checkbox" name="Activities[]" value="BirdsOfPrey" />
Birds of Prey Tour & Falconry Exhibition</td>
</tr>
<tr>
<td><input type="checkbox" name="Activities[]" value="Fishing" />
Fishing</td>
<td></td>
<td><input type="checkbox" name="Activities[]" value="BullsBayGolf" />
Golf @ Bulls Bay</td>
<td></td>
</tr>
<tr>
<td>Comments:</td>
<td colspan="2"><textarea name="Comments" rows=5 cols=45></textarea></td>
<td><input type=submit value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
Re: Emailing check box values from a form
Posted: Wed May 23, 2012 4:29 pm
by phpsounds
That did it. I managed to adjust it further to suit my needs. I can't figure out how to make the check_msg data come after the sprintf section in the email, but it's not the end of the world. Thanks so much for taking the time, really appreciate it.