开发者

send form based on value of select (php)

开发者 https://www.devze.com 2023-04-04 04:38 出处:网络
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 locat

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] = location1@domain.com
if $_POST['location'] value = "location2" then [to] = location2@domain.com
if $_POST['location'开发者_运维问答] value = "location3" then [to] = location3@domain.com
if $_POST['location'] value = "location4" then [to] = location4@domain.com

Then do something like this:

...

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

...

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

...

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


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.


You could use a switch:

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

    switch($location){
        case "location1":
            $location='email1@example.com';
            break;
        case "location2":
            $location='email2@example.com';
            break;
        case "location3":
            $location='email3@example.com';
            break;
        case "location4":
            $location='email4@example.com';
            break;

        default:
            $location='error@example.com';
            break;
    }

?>
0

精彩评论

暂无评论...
验证码 换一张
取 消