根据从下拉列表中选择的选项,将PHP表单提交到不同的地址

How can I submit a form to one of 4 different addresses depending on what the user selects from a dropdown?

So 4 options are 1,2,3,4 (in my case these are different branches) If 1 is selected I want the form to submit to 1@email.com, if 2 is selected the form should go to 2@email.com etc.

I saw similar questions on here but always seem to be database or wordpress related.

The processor for the form looks like this:

<?php
include_once "includes/funcs.php";

$success=$_GET['success'];
if(isset($_POST['submit']))
{

/* Check all form inputs using check_input function */
$name = trim(stripslashes(htmlspecialchars($_POST['InputName'])));
$email =  trim(stripslashes(htmlspecialchars($_POST['InputEmail'])));
$phone =  trim(stripslashes(htmlspecialchars($_POST['InputPhone'])));
$branch =  trim(stripslashes(htmlspecialchars($_POST['InputBranch'])));
$message =  trim(stripslashes(htmlspecialchars($_POST['InputMessage'])));

$j=0;
            $filecount=count($_FILES['upload']['name']);
            for($i=0; $i<$filecount; $i++) 
            {
//Get the temp file path

//Make sure we have a filepath
if ($_FILES['upload']['size'][$i]<=2048000){

$filep[$j]=$_FILES['upload']['tmp_name'][$i];
$filen[$j]=$_FILES['upload']['name'][$i];

$j+=1;
//echo $files[$i]."<br/><br/>";
}
            }


$subject = "You have received an enquiry";
$message = "

This is a message via the website:

Name: $name
Email: $email
Subject: $subject
Telephone: $phone
Branch: $branch

Message:
$message
";

//echo "here";

/* Send the message using mail() function */
emailPerson($subject,$message,'xx@emailaddress.com',$filep,$filen);

/* Redirect visitor to the thank you page */
$success=1;
$name = "";
$email  =  "";
$phone  =  "";
$branch =  "";
$message = "";  
header('Location: thanks-for-your-interest.php');
//exit();


}
include_once "includes/top.php";

?>

and the HTML:

<label for="InputBranch">Please select the branch you wish to send this message to:</label>
    <div class="input-group">
      <select class="form-control" name='InputBranch' id='InputBranch' required  >
        <option value="Branch 1">1</option>
        <option value="Branch 2">2</option>
        <option value="Branch 3">3</option>
        <option value="Branch 4">4</option>
      </select>
      <span class="input-group-addon"><i class="glyphicon glyphicon-ok form-control-feedback"></i></span></div>
  </div>

Currently the logic tells it to submit to xx@emailaddress.com each time and just picks up the name of the branch in the email but I need it to go to the relevant branch.

Thanks

Simple: just check what the value POSTed was for your input. In your case, it’s called InputBranch:

switch ($_POST['InputBranch']) {
    case 'Blackpool':
        $email = 'blackpool@exmaple.com';
        break;
    case 'Kendal':
        $email = 'kendal@exmaple.com';
        break;
    case 'Lancaster':
        $email = 'lancaster@exmaple.com';
        break;
    case 'Preston':
        $email = 'preston@exmaple.com';
        break;
    default:
        throw new Exception('Invalid branch selected.')
}

mail($email, $subject, $message, $headers);

Also, you don’t need to reset variables here:

$success=1;
$name = "";
$email  =  "";
$phone  =  "";
$branch =  "";
$message = "";  
header('Location: thanks-for-your-interest.php');

If you’re just going to redirect. The variables’ values won’t persist between pages.

You could do:

<?php

$recipients = array(
    'Branch 1' => 'email1@domain.com',
    'Branch 2' => 'email2@domain.com',
    'Branch 3' => 'email3@domain.com',
    'Branch 4' => 'email4@domain.com',
);

if (isset($recipients[$branch])) {
    $to = $recipients[$branch];
} else {
    // invalid branch selected
}

You could put this after you assign the variables from $_POST and it will select the recipient from the array based on the submitted form value.