【问题标题】:Display "Name" not "ID" when sending an emails发送电子邮件时显示“姓名”而不是“ID”
【发布时间】:2018-10-18 03:15:59
【问题描述】:

我是 PHP 的初学者。现在,我想设置发送电子邮件。但不幸的是,我使用“ID”的某些字段将显示“名称”。 例如:

<div class="form-group">
                            <label for="contact_departement_id"><?php echo $lang['contact_departement_id'] ?></label>
                            <select class="form-control" name="contact_departement_id" id="contact_departement_id">
                                <option value="x"><?php echo $lang['chooseone'] ?></option>
                                <?php foreach($listDepartement['datas'] as $departement): ?>
                                    <option 
                                        <?php 
                                            echo ($state == 'edit') && ($departement->departement_id == $datas->contact_departement_id) ? 'selected="selected"': '';
                                        ?>
                                        value="<?php echo $departement->departement_id ?>">
                                        <?php echo $departement->departement_name ?>
                                    </option>
                                <?php endforeach ?>
                            </select>
                        </div>  

我在电子邮件中设置

$contact_name = $_POST['contact_name'];
$contact_ext = $_POST['contact_ext'];
$contact_direktorat_id = $_POST['contact_direktorat_id'];
$contact_departement_id = $_POST['contact_departement_id'];
$contact_lokasi_id = $_POST['contact_lokasi_id'];
//$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted
$message = file_get_contents('mail.html'); 
    $message = str_replace('%contact_name%', $contact_name, $message); 
    $message = str_replace('%contact_ext%', $contact_ext, $message); 
    $message = str_replace('%contact_direktorat_id%', $contact_direktorat_id, $message); 
    $message = str_replace('%contact_departement_id%', $contact_departement_id, $message); 
    $message = str_replace('%contact_lokasi_id%', $contact_lokasi_id, $message); 

输出:

Dear All,

New Extention has been added. Here are details:
Contact Name: test
Ext: 123
Directorate: 2
Department: 2
Location: 4

我需要一些指导如何显示“姓名”而不是“ID”

【问题讨论】:

  • 要么通过id查询名称,要么将名称放在value而不是id中。
  • 我想把值而不是id放在里面

标签: php phpmailer html-email


【解决方案1】:

表单提交 option 元素的 value 属性中的任何内容,而不是标签的内容。您需要为您的姓名提供映射,例如:

$departments = [
    '1' => 'Sales',
    '2' => 'Support',
    '3' => 'Accounts',
];

您应该在使用它们之前验证提交的值(因为客户端可能会提交垃圾、黑客攻击等),然后将它们映射到数组中,例如:

if (!array_key_exists($_POST['contact_direktorat_id'], $departments)) {
    $contact_direktorat_name = $departments[$_POST['contact_direktorat_id']];
} else {
    $contact_direktorat_name = 'Unknown';
}

用你需要的任意数量重复此操作,根据需要定义任意数量的数组,或者可能在数据库中查找值以验证它们而不是使用数组(但请确保不要对 SQL 注入攻击敞开心扉) - 你明白了。

【讨论】:

  • 输出是:亲爱的,新扩展已添加。以下是详细信息: 联系人姓名:测试分机:123 局:阵列部门:26 位置:LT01 局 => 阵列
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
  • 2014-10-18
  • 2021-12-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多