Forum Moderators: coopster
New to php and have an email form question. I have a form on a photography website where a couple can select what pictures they want to have taken. Lots of checkboxes and several comment boxes. I can get the form to work by sending all of the information but I can't seem to figure out the array for it to send only the affirmative answers. I am also having a hard time associating the checkbox "Others" with the comments in the textarea "Others". Since the page is over 500 lines long, here is a snippet of my code:
<tr>
<td width="28"><input type="checkbox" name="wedding[]" value="groom_retrieving_garter"></td>
<td width="350">Groom retrieving garter</td>
<td> </td>
<td width="28"><input type="checkbox" name="wedding[]" value="groom_tossing_garter"></td>
<td width="350">Groom tossing garter</td>
</tr>
<tr>
<td width="28"><input type="checkbox" name="wedding[]" value="garter andor bouquet_dance"></td>
<td width="350">Garter/Bouquet dance</td>
<td> </td>
<td width="28"><input type="checkbox" name="wedding[]" value="the_getaway_car"></td>
<td width="350">The getaway car</td>
</tr>
<tr>
<td width="28"><input type="checkbox" name="wedding[]" value="bride_and_groom_leaving_party"></td>
<td width="350">Bride & Groom leaving party</td>
<td> </td>
<td width="28"><input type="checkbox" name="wedding[]" value="bride_and_groom_driving_away"></td>
<td width="350">Bride & Groom driving away</td>
</tr>
<tr>
<td width="28"><input type="checkbox" name="wedding[]" value="photos_of_special_guests"></td>
<td width="350">Special Guests:</td>
<td> </td>
<td width="28"> </td>
<td width="350"> </td>
</tr>
<tr>
<td width="28"> </td>
<td colspan="4"><textarea name="b4" cols="75" rows="5" id="b4"></textarea></td>
</tr>
</table>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1">
<tr>
<td width="28"></td>
<td width="350">Email From:*</td>
<td> </td>
<td width="28"><input type="text" name="EmailFrom"></td>
<td width="350"> </td>
</tr>
<tr>
<td width="28"></td>
<td width="350">First and Last Name:*</td>
<td> </td>
<td width="28"><input type="text" name="FirstandLastName"></td>
<td width="350"> </td>
</tr>
<tr>
<td> </td>
<td><div align="left">Fields marked (*) are required </div></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
<table width="755" align="center">
<tr>
<td> </td>
<td><input type="submit" value="Submit"></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
and here is my php code:
<?php
// variables
$EmailFrom = Trim(stripslashes($_POST['EmailFrom']));
$EmailTo = "Jason@example.com";
$Subject = "Our Wedding Photos";
$name = Trim(stripslashes($_POST['FirstandLastName']));
$email = Trim(stripslashes($_POST['EmailFrom']));
// validation
$validationOK=true;
if (Trim($EmailFrom)=="") $validationOK=false;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=../error.shtml\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "\n";
$Body .= "\n";
// if posted data
if ($_POST)
{
$body = "";
// go through all form elements
foreach ( $_POST as $key => $value )
{
// if element is array (checkboxes)
if ( is_array ($value))
{
$body .= $key . " = ";
foreach ($value as $hey)
$body .= $hey . ", ";
$body .= "\r\n";
// else if regular elements
} else {
// display form field = form value
$body .= $key . " = " . "$value\r\n";
}
}
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=../ok.shtml\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=../error.shtml\">";
}
}
?>
Thanks in advance for your insight!
Jason
[edited by: eelixduppy at 7:09 pm (utc) on May 10, 2008]
[edit reason] no URLs, please [/edit]
you could test for an empty value in your foreach loop using empty [php.net]
my understanding was you only wanted to "send affirmative answers". My assumption from there was if a value was empty then not to include it in the email
you could add an empty test, or any other test, to your loop that appends the data here
// go through all form elements
foreach ( $_POST as $key => $value )
is that what you wanted?