如何根据用户从下拉菜单中选择的内容,将表单提交到4个不同地址之一?
所以4个选项是1,2,3,4(在我的情况下这些是不同的分支)如果选择1我希望表单提交到1@email.com,如果选择2,表单应该转到2 @ email.com等。
我在这里看到了类似的问题,但似乎总是与数据库或wordpress相关。
表单的处理器如下所示:
<?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";
?>
和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>
目前,逻辑告诉它每次都要提交到xx@emailaddress.com,只是在电子邮件中选择分支的名称,但我需要它去相关分支。
由于
答案 0 :(得分:1)
简单:只需检查POSTed的输入值。在您的情况下,它被称为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);
此外,您无需在此处重置变量:
$success=1;
$name = "";
$email = "";
$phone = "";
$branch = "";
$message = "";
header('Location: thanks-for-your-interest.php');
如果您要重定向。变量的值不会在页面之间保留。
答案 1 :(得分:0)
你可以这样做:
<?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
}
您可以在从$_POST
分配变量后将其放入,并根据提交的表单值从数组中选择收件人。