Basic form - email sent based on drop down option selected
- jennifer420
- Born


- Joined: Jul 16, 2009
- Posts: 3
- Status: Offline
Hi,
My apologies if this is already posted, I've searched everywhere (google, etc) as well as reading this post: website-design-forum/multiple-email-addresses-one-form-t21891.html but just can't seem to get it right.
My form has a drop down in it where people may select a city ... and each city has it's own email.
For example:
If city 'Mathis' is selected, then an email will go to x@mathis.com
If city 'Orange Grove' is selected, then an email will go to x@orangegrove.com
and so on ...
Below is where I'm at with he form, but I can't get this to work.
I'd really appreciate any help with this, thank you so much ...
Jennifer
My apologies if this is already posted, I've searched everywhere (google, etc) as well as reading this post: website-design-forum/multiple-email-addresses-one-form-t21891.html but just can't seem to get it right.
My form has a drop down in it where people may select a city ... and each city has it's own email.
For example:
If city 'Mathis' is selected, then an email will go to x@mathis.com
If city 'Orange Grove' is selected, then an email will go to x@orangegrove.com
and so on ...
Below is where I'm at with he form, but I can't get this to work.
I'd really appreciate any help with this, thank you so much ...
Code: [ Select ]
<?php
$EmailFrom = "xxx";
$EmailTo = "xxx";
if (Locations===Marthis) {
mailto = "x@mathis.com";
} else if (Locations===Orange Grove) {
mailto = "x@og.com";
}
$Subject = "Customer Request";
$locations= htmlspecialchars(Trim(stripslashes($_POST['Locations'])));
$Name = htmlspecialchars(Trim(stripslashes($_POST['Name'])));
$Phone = htmlspecialchars(Trim(stripslashes($_POST['Phone'])));
$Email = htmlspecialchars(Trim(stripslashes($_POST['Email'])));
$Message = htmlspecialchars(Trim(stripslashes($_POST['Message'])));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Phone;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Location: ";
$Body .= $Locations;
$Body .= "\n";
$Body .= "Request: ";
$Body .= $Request;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=submit.htm\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
$EmailFrom = "xxx";
$EmailTo = "xxx";
if (Locations===Marthis) {
mailto = "x@mathis.com";
} else if (Locations===Orange Grove) {
mailto = "x@og.com";
}
$Subject = "Customer Request";
$locations= htmlspecialchars(Trim(stripslashes($_POST['Locations'])));
$Name = htmlspecialchars(Trim(stripslashes($_POST['Name'])));
$Phone = htmlspecialchars(Trim(stripslashes($_POST['Phone'])));
$Email = htmlspecialchars(Trim(stripslashes($_POST['Email'])));
$Message = htmlspecialchars(Trim(stripslashes($_POST['Message'])));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Phone;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Location: ";
$Body .= $Locations;
$Body .= "\n";
$Body .= "Request: ";
$Body .= $Request;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=submit.htm\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
- <?php
- $EmailFrom = "xxx";
- $EmailTo = "xxx";
- if (Locations===Marthis) {
- mailto = "x@mathis.com";
- } else if (Locations===Orange Grove) {
- mailto = "x@og.com";
- }
- $Subject = "Customer Request";
- $locations= htmlspecialchars(Trim(stripslashes($_POST['Locations'])));
- $Name = htmlspecialchars(Trim(stripslashes($_POST['Name'])));
- $Phone = htmlspecialchars(Trim(stripslashes($_POST['Phone'])));
- $Email = htmlspecialchars(Trim(stripslashes($_POST['Email'])));
- $Message = htmlspecialchars(Trim(stripslashes($_POST['Message'])));
- // validation
- $validationOK=true;
- if (!$validationOK) {
- print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
- exit;
- }
- // prepare email body text
- $Body = "";
- $Body .= "Name: ";
- $Body .= $Name;
- $Body .= "\n";
- $Body .= "Phone: ";
- $Body .= $Phone;
- $Body .= "\n";
- $Body .= "Email: ";
- $Body .= $Email;
- $Body .= "\n";
- $Body .= "Location: ";
- $Body .= $Locations;
- $Body .= "\n";
- $Body .= "Request: ";
- $Body .= $Request;
- $Body .= "\n";
- // send email
- $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
- // redirect to success page
- if ($success){
- print "<meta http-equiv=\"refresh\" content=\"0;URL=submit.htm\">";
- }
- else{
- print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
- }
- ?>
Jennifer
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
July 16th, 2009, 12:19 pm
- UPSGuy
- Lurker ಠ_ಠ


- Joined: Jul 25, 2005
- Posts: 2735
- Loc: Nashville, TN
- Status: Offline
Code: [ Select ]
if (Locations===Marthis) {
mailto = "x@mathis.com";
} else if (Locations===Orange Grove) {
mailto = "x@og.com";
}
- if (Locations===Marthis) {
- mailto = "x@mathis.com";
- } else if (Locations===Orange Grove) {
- mailto = "x@og.com";
- }
What is Locations supposed to be? If it's from the form, then this is more what you're looking for:
Code: [ Select ]
<?php
$EmailFrom = "xxx";
$EmailTo = "xxx";
$Subject = "Customer Request";
$Locations= htmlspecialchars(trim(stripslashes($_POST['Locations'])));
$Name = htmlspecialchars(trim(stripslashes($_POST['Name'])));
$Phone = htmlspecialchars(trim(stripslashes($_POST['Phone'])));
$Email = htmlspecialchars(trim(stripslashes($_POST['Email'])));
$Message = htmlspecialchars(trim(stripslashes($_POST['Message'])));
switch($Locations) {
case "Marthis":
$mailto = "x@mathis.com";
break;
case "Orange Grove":
$mailto = "x@og.com";
break;
default:
$mailto = "yourSiteAdminAddyHereToCatchInvalidLocations@site.com";
break;
}
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "Name: $Name\n";
$Body .= "Phone: $Phone\n";
$Body .= "Email: $Email\n";
$Body .= "Location: $Locations\n";
$Body .= "Request: $Request\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=submit.htm\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
- <?php
- $EmailFrom = "xxx";
- $EmailTo = "xxx";
- $Subject = "Customer Request";
- $Locations= htmlspecialchars(trim(stripslashes($_POST['Locations'])));
- $Name = htmlspecialchars(trim(stripslashes($_POST['Name'])));
- $Phone = htmlspecialchars(trim(stripslashes($_POST['Phone'])));
- $Email = htmlspecialchars(trim(stripslashes($_POST['Email'])));
- $Message = htmlspecialchars(trim(stripslashes($_POST['Message'])));
- switch($Locations) {
- case "Marthis":
- $mailto = "x@mathis.com";
- break;
- case "Orange Grove":
- $mailto = "x@og.com";
- break;
- default:
- $mailto = "yourSiteAdminAddyHereToCatchInvalidLocations@site.com";
- break;
- }
- // validation
- $validationOK=true;
- if (!$validationOK) {
- print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
- exit;
- }
- // prepare email body text
- $Body = "Name: $Name\n";
- $Body .= "Phone: $Phone\n";
- $Body .= "Email: $Email\n";
- $Body .= "Location: $Locations\n";
- $Body .= "Request: $Request\n";
- // send email
- $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
- // redirect to success page
- if ($success){
- print "<meta http-equiv=\"refresh\" content=\"0;URL=submit.htm\">";
- }
- else{
- print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
- }
- ?>
I condensed your body content, swapped your if's to a proper switch/case.
Three things you should remember:
1) PHP is case-sensitive (Trim != trim)
2) Variables begin with a dollar sign
3) Literal strings must be enclosed in quotations
I'd love to change the world, but they won't give me the source code.
- jennifer420
- Born


- Joined: Jul 16, 2009
- Posts: 3
- Status: Offline
Hi UPSGuy,
Thank you ... yes, it's from my form.
I still can't get it right though, I've entered various emails for the different options but they still don't seem to be going where they're supposed to.
$EmailFrom = "xxx";
$EmailTo = "xxx";
$Subject = "Customer Request";
$Locations= htmlspecialchars(trim(stripslashes($_POST['Locations'])));
$Name = htmlspecialchars(trim(stripslashes($_POST['Name'])));
$Phone = htmlspecialchars(trim(stripslashes($_POST['Phone'])));
$Email = htmlspecialchars(trim(stripslashes($_POST['Email'])));
$Message = htmlspecialchars(trim(stripslashes($_POST['Message'])));
switch($Locations) {
case "Mathis":
$mailto = "x@mathis.com";
break;
case "Orange Grove":
$mailto = "x@orangegrove.com";
break;}
This is much more complicated then I thought!
I appreciate you taking the time to help, thanks so much `
Jennifer
Thank you ... yes, it's from my form.
I still can't get it right though, I've entered various emails for the different options but they still don't seem to be going where they're supposed to.
Code: [ Select ]
$EmailFrom = "xxx";
$EmailTo = "xxx";
$Subject = "Customer Request";
$Locations= htmlspecialchars(trim(stripslashes($_POST['Locations'])));
$Name = htmlspecialchars(trim(stripslashes($_POST['Name'])));
$Phone = htmlspecialchars(trim(stripslashes($_POST['Phone'])));
$Email = htmlspecialchars(trim(stripslashes($_POST['Email'])));
$Message = htmlspecialchars(trim(stripslashes($_POST['Message'])));
switch($Locations) {
case "Mathis":
$mailto = "x@mathis.com";
break;
case "Orange Grove":
$mailto = "x@orangegrove.com";
break;}
- $EmailFrom = "xxx";
- $EmailTo = "xxx";
- $Subject = "Customer Request";
- $Locations= htmlspecialchars(trim(stripslashes($_POST['Locations'])));
- $Name = htmlspecialchars(trim(stripslashes($_POST['Name'])));
- $Phone = htmlspecialchars(trim(stripslashes($_POST['Phone'])));
- $Email = htmlspecialchars(trim(stripslashes($_POST['Email'])));
- $Message = htmlspecialchars(trim(stripslashes($_POST['Message'])));
- switch($Locations) {
- case "Mathis":
- $mailto = "x@mathis.com";
- break;
- case "Orange Grove":
- $mailto = "x@orangegrove.com";
- break;}
This is much more complicated then I thought!
I appreciate you taking the time to help, thanks so much `
Jennifer
- UPSGuy
- Lurker ಠ_ಠ


- Joined: Jul 25, 2005
- Posts: 2735
- Loc: Nashville, TN
- Status: Offline
Start with something simpler. At the very top of your file, add the following lines and check to see that everything you're expecting is getting passed in as intended:
Code: [ Select ]
foreach($_REQUEST as $key => $val) {
echo "$key: $val<br/>";
}
exit();
echo "$key: $val<br/>";
}
exit();
- foreach($_REQUEST as $key => $val) {
- echo "$key: $val<br/>";
- }
- exit();
I'd love to change the world, but they won't give me the source code.
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8211
- Loc: USA
- Status: Offline
Here is another way you could do it:
the_form.html
submitted.php
Just add fields to this and your validation... I already stripping and trimming every submitted value here, so that should be ok.
the_form.html
HTML Code: [ Select ]
<form action="submitted.php" method="post">
Location: <select name="location">
<option value="0">Kansas</option>
<option value="1">Alaska</option>
<option value="2">Alabama</option>
<option value="3">Kentucky</option>
<option value="4">New Jersey</option>
<option value="5">California</option>
</select>
<input type="submit" name="submit" value="Submit" /></form>
Location: <select name="location">
<option value="0">Kansas</option>
<option value="1">Alaska</option>
<option value="2">Alabama</option>
<option value="3">Kentucky</option>
<option value="4">New Jersey</option>
<option value="5">California</option>
</select>
<input type="submit" name="submit" value="Submit" /></form>
- <form action="submitted.php" method="post">
- Location: <select name="location">
- <option value="0">Kansas</option>
- <option value="1">Alaska</option>
- <option value="2">Alabama</option>
- <option value="3">Kentucky</option>
- <option value="4">New Jersey</option>
- <option value="5">California</option>
- </select>
- <input type="submit" name="submit" value="Submit" /></form>
submitted.php
PHP Code: [ Select ]
function strip_htmltags($array)
{
$return = array();
foreach($array as $key => $value)
{
$return[$key] = htmlspecialchars(trim($value));
}
return $return;
}
$_POST = strip_htmltags($_POST);
$locations = array(
'0' => <!-- e -->'x@kansas.com<!-- e -->',
'1' => <!-- e -->'x@alaska.com<!-- e -->',
'2' => <!-- e -->'x@alabama.com<!-- e -->',
'3' => <!-- e -->'x@kentucky.com<!-- e -->',
'4' => <!-- e -->'x@new.jersy.com<!-- e -->',
'5' => <!-- e -->'x@california.com<!-- e -->');
$location = $_POST['location'];
$email = $locations[$location];
echo $email;
{
$return = array();
foreach($array as $key => $value)
{
$return[$key] = htmlspecialchars(trim($value));
}
return $return;
}
$_POST = strip_htmltags($_POST);
$locations = array(
'0' => <!-- e -->'x@kansas.com<!-- e -->',
'1' => <!-- e -->'x@alaska.com<!-- e -->',
'2' => <!-- e -->'x@alabama.com<!-- e -->',
'3' => <!-- e -->'x@kentucky.com<!-- e -->',
'4' => <!-- e -->'x@new.jersy.com<!-- e -->',
'5' => <!-- e -->'x@california.com<!-- e -->');
$location = $_POST['location'];
$email = $locations[$location];
echo $email;
- function strip_htmltags($array)
- {
- $return = array();
- foreach($array as $key => $value)
- {
- $return[$key] = htmlspecialchars(trim($value));
- }
- return $return;
- }
- $_POST = strip_htmltags($_POST);
- $locations = array(
- '0' => <!-- e -->'x@kansas.com<!-- e -->',
- '1' => <!-- e -->'x@alaska.com<!-- e -->',
- '2' => <!-- e -->'x@alabama.com<!-- e -->',
- '3' => <!-- e -->'x@kentucky.com<!-- e -->',
- '4' => <!-- e -->'x@new.jersy.com<!-- e -->',
- '5' => <!-- e -->'x@california.com<!-- e -->');
- $location = $_POST['location'];
- $email = $locations[$location];
- echo $email;
Just add fields to this and your validation... I already stripping and trimming every submitted value here, so that should be ok.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 5 posts
- Users browsing this forum: No registered users and 168 guests
- You cannot post new topics in this forum
- You cannot reply to topics in this forum
- You cannot edit your posts in this forum
- You cannot delete your posts in this forum
- You cannot post attachments in this forum
