当前位置: 动力学知识库 > 问答 > 编程问答 >

arrays - Sending PHP form information to different email addresses based on user selection

问题描述:

Currently trying to modify a PHP contact form plugin. I need for users to be able to select which office department they'd like to send their email to, and have the contact info. go to that specific email address.

This plugin has an array in the PHP that allows for sending the contact form info. to multiple email addresses. The problem is that it sends the info. to EVERY email address in the array, and I need for users to be able to select just one address in the array to send the info. to.

Here is the array in the PHP:

$recipients = true;

if($recipients == true){

$recipients = array(

'Select department you are trying to reach*' => '',

"[email protected]" => "Parts",

"[email protected]" => "Sales",

"[email protected]" => "Service",

);

foreach($recipients as $email => $name){

$mail->AddBCC($email, $name);

}

}

Here is the contact form's setup:

<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>

How would I add a dropdown form to this contact form, so someone could specifically choose the department (and coinciding email address from the array) they'd like to send the email to?

Help is much appreciated. I'm completely stumped about how to achieve this. For anyone curious, the plugin I'm using is called "Smart Forms", and they have a demo here: http://codecanyon.net/item/smart-forms/full_screen_preview/7254656.

网友答案:

Loop through your possible recipients on your form like this:

<?php 
$recipients = array(
        '' => 'Select department you are trying to reach*',
        "[email protected]" => "Parts",
        "[email protected]" => "Sales",
        "[email protected]" => "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> 

And then in the submission script smartprocess.php check and see if the submitted email address matches one of your recipients :

<?php 
    $recipients = array(
            '' => 'Select department you are trying to reach*',
            "[email protected]" => "Parts",
            "[email protected]" => "Sales",
            "[email protected]" => "Service",
    );
    if( $_POST['to-emailaddress'] && $recipients[$_POST['to-emailaddress']]){
        $name = $recipients[$_POST['to-emailaddress']];
        $email = $_POST['to-emailaddress'];
        $mail->AddBCC($email, $name);
    }
?>
分享给朋友:
您可能感兴趣的文章:
随机阅读: