Basic form - email sent based on drop down option selected

  • jennifer420
  • Born
  • Born
  • No Avatar
  • Joined: Jul 16, 2009
  • Posts: 3
  • Status: Offline

Post July 16th, 2009, 12:19 pm

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 ...

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\">";
}
?>
 
  1. <?php
  2.  
  3. $EmailFrom = "xxx";
  4. $EmailTo = "xxx";
  5. if (Locations===Marthis) {
  6.  
  7.      mailto = "x@mathis.com";
  8.  
  9. } else if (Locations===Orange Grove) {
  10.  
  11.      mailto = "x@og.com";
  12.  
  13. }
  14. $Subject = "Customer Request";
  15. $locations= htmlspecialchars(Trim(stripslashes($_POST['Locations'])));
  16. $Name = htmlspecialchars(Trim(stripslashes($_POST['Name'])));
  17. $Phone = htmlspecialchars(Trim(stripslashes($_POST['Phone'])));
  18. $Email = htmlspecialchars(Trim(stripslashes($_POST['Email'])));
  19. $Message = htmlspecialchars(Trim(stripslashes($_POST['Message'])));
  20.  
  21. // validation
  22. $validationOK=true;
  23. if (!$validationOK) {
  24.   print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  25.   exit;
  26. }
  27.  
  28. // prepare email body text
  29. $Body = "";
  30. $Body .= "Name: ";
  31. $Body .= $Name;
  32. $Body .= "\n";
  33. $Body .= "Phone: ";
  34. $Body .= $Phone;
  35. $Body .= "\n";
  36. $Body .= "Email: ";
  37. $Body .= $Email;
  38. $Body .= "\n";
  39. $Body .= "Location: ";
  40. $Body .= $Locations;
  41. $Body .= "\n";
  42. $Body .= "Request: ";
  43. $Body .= $Request;
  44. $Body .= "\n";
  45.  
  46.  
  47. // send email
  48. $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
  49.  
  50. // redirect to success page
  51. if ($success){
  52.   print "<meta http-equiv=\"refresh\" content=\"0;URL=submit.htm\">";
  53. }
  54. else{
  55.   print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  56. }
  57. ?>
  58.  


Jennifer
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post July 16th, 2009, 12:19 pm

  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post July 16th, 2009, 12:33 pm

Code: [ Select ]
 
 if (Locations===Marthis) {
 
      mailto = "x@mathis.com";
 
 } else if (Locations===Orange Grove) {
 
      mailto = "x@og.com";
 
 }
 
  1.  
  2.  if (Locations===Marthis) {
  3.  
  4.       mailto = "x@mathis.com";
  5.  
  6.  } else if (Locations===Orange Grove) {
  7.  
  8.       mailto = "x@og.com";
  9.  
  10.  }
  11.  


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\">";
}
?>
 
  1.  
  2. <?php
  3.  
  4. $EmailFrom = "xxx";
  5. $EmailTo = "xxx";
  6. $Subject = "Customer Request";
  7. $Locations= htmlspecialchars(trim(stripslashes($_POST['Locations'])));
  8. $Name = htmlspecialchars(trim(stripslashes($_POST['Name'])));
  9. $Phone = htmlspecialchars(trim(stripslashes($_POST['Phone'])));
  10. $Email = htmlspecialchars(trim(stripslashes($_POST['Email'])));
  11. $Message = htmlspecialchars(trim(stripslashes($_POST['Message'])));
  12.  
  13. switch($Locations) {
  14.     case "Marthis":
  15.         $mailto = "x@mathis.com";
  16.         break;
  17.     case "Orange Grove":
  18.         $mailto = "x@og.com";
  19.         break;
  20.     default:
  21.         $mailto = "yourSiteAdminAddyHereToCatchInvalidLocations@site.com";
  22.         break;
  23. }
  24.  
  25. // validation
  26. $validationOK=true;
  27. if (!$validationOK) {
  28.   print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  29.   exit;
  30. }
  31.  
  32. // prepare email body text
  33. $Body = "Name: $Name\n";
  34. $Body .= "Phone: $Phone\n";
  35. $Body .= "Email: $Email\n";
  36. $Body .= "Location: $Locations\n";
  37. $Body .= "Request: $Request\n";
  38.  
  39. // send email
  40. $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
  41.  
  42. // redirect to success page
  43. if ($success){
  44.   print "<meta http-equiv=\"refresh\" content=\"0;URL=submit.htm\">";
  45. }
  46. else{
  47.   print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  48. }
  49. ?>
  50.  


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
  • Born
  • No Avatar
  • Joined: Jul 16, 2009
  • Posts: 3
  • Status: Offline

Post July 16th, 2009, 12:57 pm

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.

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;}
  1.  
  2. $EmailFrom = "xxx";
  3. $EmailTo = "xxx";
  4. $Subject = "Customer Request";
  5. $Locations= htmlspecialchars(trim(stripslashes($_POST['Locations'])));
  6.  $Name = htmlspecialchars(trim(stripslashes($_POST['Name'])));
  7.  $Phone = htmlspecialchars(trim(stripslashes($_POST['Phone'])));
  8.  $Email = htmlspecialchars(trim(stripslashes($_POST['Email'])));
  9.  $Message = htmlspecialchars(trim(stripslashes($_POST['Message'])));
  10.  
  11.  switch($Locations) {
  12.      case "Mathis":
  13.          $mailto = "x@mathis.com";
  14.          break;
  15.      case "Orange Grove":
  16.          $mailto = "x@orangegrove.com";
  17.          break;}


This is much more complicated then I thought!

I appreciate you taking the time to help, thanks so much `

Jennifer
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post July 16th, 2009, 1:16 pm

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();
  1. foreach($_REQUEST as $key => $val) {
  2.     echo "$key: $val<br/>";
  3. }
  4. exit();
I'd love to change the world, but they won't give me the source code.
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post July 17th, 2009, 2:02 pm

Here is another way you could do it:
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>
  1. <form action="submitted.php" method="post">
  2. Location: <select name="location">
  3. <option value="0">Kansas</option>
  4. <option value="1">Alaska</option>
  5. <option value="2">Alabama</option>
  6. <option value="3">Kentucky</option>
  7. <option value="4">New Jersey</option>
  8. <option value="5">California</option>
  9. </select>
  10. <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;
  1. function strip_htmltags($array)
  2. {
  3.    $return = array();
  4.    foreach($array as $key => $value)
  5.    {
  6.       $return[$key] = htmlspecialchars(trim($value));
  7.    }
  8.    return $return;
  9. }
  10.  
  11. $_POST = strip_htmltags($_POST);
  12.  
  13. $locations = array(
  14. '0' => <!-- e -->'x@kansas.com<!-- e -->',
  15. '1' => <!-- e -->'x@alaska.com<!-- e -->',
  16. '2' => <!-- e -->'x@alabama.com<!-- e -->',
  17. '3' => <!-- e -->'x@kentucky.com<!-- e -->',
  18. '4' => <!-- e -->'x@new.jersy.com<!-- e -->',
  19. '5' => <!-- e -->'x@california.com<!-- e -->');
  20.  
  21. $location = $_POST['location'];
  22.  
  23. $email = $locations[$location];
  24.  
  25. 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

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 123 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
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.