簡體   English   中英

將PHP表單提交到不同的地址,具體取決於從下拉列表中選擇的選項

[英]Submit PHP form to different addresses dependant on option selected from dropdown

如何根據用戶從下拉菜單中選擇的內容向四個不同的地址之一提交表單?

因此,有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,只是在電子郵件中選擇分支的名稱,但是我需要它去相關的分支。

謝謝

簡單:只需檢查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');

如果您只是要重定向。 變量的值不會在頁面之間保留。

您可以這樣做:

<?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分配變量后放置它,它將根據提交的表單值從數組中選擇收件人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM