如何將表單中的html下拉清單與php郵件連接?
P粉215292716
P粉215292716 2024-03-28 10:36:26
0
1
404

用 php 程式碼檢查此 html。這個怎麼做?我是初學者。

我正在製作一個帶有下拉式選單的聯絡表單,用於選擇特定醫生的電子郵件地址。我正在使用模板工作,在 PHP 方面沒有太多經驗。

HTML 程式碼:

#
<div class="col-md-6 form-group mb-5">
  <label for="package" >Package *</label>
  <select id="package" class="form-control" name="package">
    <option class="form-control" disabled selected value> -- select an option -- </option>
    <option value="value1">pkg1</option>
    <option value="value2">pkg2</option>
    <option value="value3">pkg3</option>
    <option value="value4">pkg4</option>
    <option value="value5">pkg5</option>
    <option value="value6">pkg6</option>
    <option value="value7">pkg7</option>
  </select>
</div>

PHP 程式碼:

#
<?php
// Get data from form 
$name = $_POST['name'];
$email= $_POST['email'];
$message= $_POST['message'];
$package = $_POST['package'];
    switch ($package) {
        case 'value1':
        $mail_to = 'your Visiting1 email';
        break;
    case 'value2':
        $mail_to = 'your Visiting2 email';
        break;
    case 'value3':
        $mail_to = 'your Visiting3 email';
        break;
}
 
$to = "mail@mail.com";
$subject="This message came from dc-ec.in Contact page";
 
// The following text will be sent
// Name = user entered name
// Email = user entered email
// Message = user entered message

$txt ="Name = ". $name . "\r\n Email = "
    . $email . "\r\n Subject = " . $mail_to . "\r\n Subject = " . $package . "\r\n Message =" . $message;
 
$headers = "From: no-reply@domain.in" . "\r\n" .
            "CC: name@domain.in";
if($email != NULL) {
    mail($to, $subject, $txt, $headers);
}
 
// Redirect to
header("Location:contact.html");
?>

P粉215292716
P粉215292716

全部回覆(1)
P粉511749537

新增如下所示的 If 條件,以便根據下拉清單選擇傳送郵件

if($_POST['package'] == "value1")
{
    $mail->Subject = 'This message came ....'; 
    $mail->addAddress('test1@gmail.com', 'value 1');
}
else if($_POST['package'] == "value2")
{
    $mail->Subject = 'This message came from..'; 
    $mail->addAddress('test2@gmail.com', 'value 2');  
}
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!