根据用户选择将

时间:2015-11-19 16:18:56

标签: php arrays contact-form

目前正在尝试修改PHP联系表单插件。我需要用户能够选择他们想要将电子邮件发送到哪个办公室部门,并获得联系信息。转到该特定电子邮件地址。

此插件在PHP中有一个数组,允许发送联系表单信息。到多个电子邮件地址。问题是它发送信息。到阵列中的每个电子邮件地址,我需要用户能够只选择阵列中的一个地址来发送信息。至。

以下是PHP中的数组:

$recipients = true;
    if($recipients == true){
        $recipients = array(

        'Select department you are trying to reach*' => '',
        "example1@gmail.com" => "Parts",
        "example2@gmail.com" => "Sales",
        "example3@gmail.com" => "Service",
        );

        foreach($recipients as $email => $name){
            $mail->AddBCC($email, $name);
        }   
    }

以下是联系表单的设置:

<form method="post" action="php/smartprocess.php" id="smart-form">

    <div class="form-body">


        <label class="field prepend-icon">
            <input type="text" name="sendername" id="sendername" class="gui-input" placeholder="Enter name">
        </label>

        <label class="field prepend-icon">
            <input type="email" name="emailaddress" id="emailaddress" class="gui-input" placeholder="Email address">
        </label>

        <label class="field">
            <input type="text" name="captcha" id="captcha" class="gui-input sfcode" maxlength="6" placeholder="Enter CAPTCHA">
        </label>

        <label class="button captcode">
            <img src="php/captcha/captcha.php?<?php echo time();?>" id="captchax" alt="captcha">
            <span class="refresh-captcha"><i class="fa fa-refresh"></i></span>
       </label>

       <div class="result"></div>

      <button type="submit" data-btntext-sending="Sending..." class="button btn-primary">Submit</button>
      <button type="reset" class="button"> Cancel </button>


</form> 

如何在此联系表单中添加下拉表单,以便有人可以专门选择他们想要发送电子邮件的部门(以及数组中的重合电子邮件地址)?

非常感谢帮助。我完全不知道如何实现这一目标。对于任何好奇的人来说,我使用的插件名为&#34; Smart Forms&#34;,他们在这里有一个演示:http://codecanyon.net/item/smart-forms/full_screen_preview/7254656

1 个答案:

答案 0 :(得分:2)

在表单上循环播放您可能的收件人:

<?php 
$recipients = array(
        '' => 'Select department you are trying to reach*',
        "example1@gmail.com" => "Parts",
        "example2@gmail.com" => "Sales",
        "example3@gmail.com" => "Service",
    );
?>

<form method="post" action="php/smartprocess.php" id="smart-form">

    <div class="form-body">


        <label class="field prepend-icon">
            <input type="text" name="sendername" id="sendername" class="gui-input" placeholder="Enter name">
        </label>

        <label class="field prepend-icon">
            <input type="email" name="emailaddress" id="emailaddress" class="gui-input" placeholder="Email address">
        </label>

        <label class="field select-to-email">
            <select name="to-emailaddress" id="toemailaddress" class="gui-input" placeholder="To Email address">
            <?php foreach($recipients as $email => $name) : ?>
                    <option value="<?php echo $email; ?>"><?php echo $name; ?></option>
            <?php endforeach; ?>
            </select>
        </label>

        <label class="field">
            <input type="text" name="captcha" id="captcha" class="gui-input sfcode" maxlength="6" placeholder="Enter CAPTCHA">
        </label>

        <label class="button captcode">
            <img src="php/captcha/captcha.php?<?php echo time();?>" id="captchax" alt="captcha">
            <span class="refresh-captcha"><i class="fa fa-refresh"></i></span>
       </label>

       <div class="result"></div>

      <button type="submit" data-btntext-sending="Sending..." class="button btn-primary">Submit</button>
      <button type="reset" class="button"> Cancel </button>


</form> 

然后在提交脚本smartprocess.php中检查并查看提交的电子邮件地址是否与您的某个收件人匹配:

<?php 
    $recipients = array(
            '' => 'Select department you are trying to reach*',
            "example1@gmail.com" => "Parts",
            "example2@gmail.com" => "Sales",
            "example3@gmail.com" => "Service",
    );
    if( $_POST['to-emailaddress'] && $recipients[$_POST['to-emailaddress']]){
        $name = $recipients[$_POST['to-emailaddress']];
        $email = $_POST['to-emailaddress'];
        $mail->AddBCC($email, $name);
    }
?>