user557372
user557372

Reputation: 21

send form based on value of select (php)

I've got a simple form that I am processing with PHP. In the processing script, I currently have hardcoded the [to] email address. I want to add a select field to the form that contains a list of locations... and based on the location selected... send the form to a specific email address. For example:

<select id="location">
  <option value="location1">Location 1</option>
  <option value="location2">Location 2</option>
  <option value="location3">Location 3</option>
  <option value="location4">Location 4</option>
</select>

Each location would have a different email address... so the form contents would be sent to the specified location.

Seems simple enough... but I'm totally lost. Do I change the value of the options to their respected email address? I didn't want to do that because it would expose the emails in the code. I'd like to assign the email addresses in the processing script through a series if if statements like:

if $_POST['location'] value = "location1" then [to] = [email protected]
if $_POST['location'] value = "location2" then [to] = [email protected]
if $_POST['location'] value = "location3" then [to] = [email protected]
if $_POST['location'] value = "location4" then [to] = [email protected]

Then do something like this:

...

$location = trim(stripslashes($_POST['location']));

...

$email['to'] = "{$location}";

...

Can somebody help/advise on the proper setup for this functionality?

Upvotes: 0

Views: 906

Answers (2)

Lawrence Cherone
Lawrence Cherone

Reputation: 46620

You could use a switch:

<?php 
    $location = $_POST['location'];

    switch($location){
        case "location1":
            $location='[email protected]';
            break;
        case "location2":
            $location='[email protected]';
            break;
        case "location3":
            $location='[email protected]';
            break;
        case "location4":
            $location='[email protected]';
            break;

        default:
            $location='[email protected]';
            break;
    }

?>

Upvotes: 0

inlinestyle
inlinestyle

Reputation: 127

What if you just make an associative array:

for every pair of locations and email you do

arr[$location] = $email;

Then once you get your $_POST, you do:

$email_address = $arr[$_POST['location']];

and now you have your email.

Upvotes: 2

Related Questions