【问题标题】:Sending PHP form information to different email addresses based on user selection根据用户选择将 PHP 表单信息发送到不同的电子邮件地址
【发布时间】:2015-11-20 00:29:31
【问题描述】:

目前正在尝试修改 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> 

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

非常感谢您的帮助。我完全不知道如何实现这一目标。对于任何好奇的人,我使用的插件称为“智能表单”,他们在这里有一个演示:http://codecanyon.net/item/smart-forms/full_screen_preview/7254656

【问题讨论】:

    标签: php arrays contact-form


    【解决方案1】:

    像这样在您的表单上遍历您可能的收件人:

    <?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);
        }
    ?>
    

    【讨论】:

    • 天哪,谢谢你,你真是个天才!几天来我一直在尝试解决这个问题,您在 5 分钟内解决了我的问题 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    相关资源
    最近更新 更多