联系表格,下拉联系人

时间:2017-09-25 21:25:38

标签: javascript php html forms email

我正在为我儿子的学校/教堂进行网站设计。他们有3个主要部门:教堂,学校和日托。他们希望用户能够使用联系表单中的下拉菜单向每个部门发送电子邮件。我以为我有它工作,但经过测试,它似乎只是通过电子邮件发送到我设置为电子邮件的默认电子邮件,如果没有人从菜单中选择一个部门。因此,正在处理和发送电子邮件表单,但它只是通过电子邮件发送到默认值。任何想法我怎么能让这个工作?我是一名网页设计师/平面设计师,对PHP没有多少经验。

以下是我的代码: HTML:

<form name="sentMessage" id="contactForm" novalidate>
    <div class="row control-group">
        <div class="form-group col-xs-12 floating-label-form-group controls">
            <label>Name</label>
            <input type="text" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
            <p class="help-block text-danger"></p>
        </div>
    </div>
    <div class="row control-group">
        <div class="form-group col-xs-12 floating-label-form-group controls">
            <label>Email Address</label>
            <input type="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
            <p class="help-block text-danger"></p>
        </div>
    </div>
    <div class="row control-group">
        <div class="form-group col-xs-12 floating-label-form-group controls">
            <label>Phone Number</label>
            <input type="tel" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number.">
            <p class="help-block text-danger"></p>
        </div>
    </div>
    <div class="row control-group">
        <div class="form-group col-xs-12 floating-label-form-group controls">
            <label>Who do you wish to contact?</label>
            <select class="form-control empty" id="selectDept" name="deptid" required data-validation-required-message="Please make a selection.">
                <option>SELECT...</option>
                <option value="church">Faith Lutheran Church</option>
                <option value="school">K-5 School</option>
                <option value="preschool">Touching Hearts Preschool</option>
            </select>
        </div>
    </div>
    <div class="row control-group">
        <div class="form-group col-xs-12 floating-label-form-group controls">
            <label>Message</label>
            <textarea rows="5" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>
            <p class="help-block text-danger"></p>
        </div>
    </div>
    <br> 

    <img id="captcha" src="/securimage/securimage_show.php" alt="CAPTCHA Image" />
    <input type="text" name="captcha_code" size="10" id="captcha_code" maxlength="6" />
        <a href="#" onclick="document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false"><span class="captchaImage"><i class="fa fa-refresh"></i></span></a>

    <br>
    <br>
    <div id="success"></div>
    <div class="row">
        <div class="form-group col-xs-12">
            <button type="submit" class="btn btn-success btn-lg">Send</button>
        </div>
    </div>
</form>

PHP:

<?php
session_start();
$response = array('error' => true, 'message' => 'OK');

// Check for empty fields
if(empty($_POST['name'])        ||
empty($_POST['email'])      ||
empty($_POST['phone'])      ||
empty($_POST['message'])    ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
    $response['message'] = "No arguments Provided!";
    die(json_encode($response));
}

include_once $_SERVER['DOCUMENT_ROOT'] . '/securimage/securimage.php';
$securimage = new Securimage();

if ($securimage->check($_POST['captcha_code']) == false) {
  // the code was incorrect
    $response['message'] = "The security code entered was incorrect.";
    die(json_encode($response));
}

$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];

// Create the email and send the message
//$to = 'info@faithlutheran.org'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
switch ($_POST['deptid']) {
    case "church":
        $to = "church@domain.com";
        break;
    case "school":
        $to = "school@domain.com";
        break;
    case "preschool":
        $to = "preschool@domain.com";
        break;
    default:
        $to = "info@domain.com";
        break;
}
$email_subject = "General Question from Website:  $name";
$email_body = "You have received a new message from Faith Lutheran website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply@domain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address"; 
mail($to,$email_subject,$email_body,$headers);

$response['error'] = false;

die(json_encode($response));

?>

使用Javascript:

$(function() {

    $("input,textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {
            // additional error messages or events
        },
        submitSuccess: function($form, event) {
            // Prevent spam click and default submit behaviour
            $("#btnSubmit").attr("disabled", true);
            event.preventDefault();

            // get values from FORM
            var name = $("input#name").val();
            var email = $("input#email").val();
            var phone = $("input#phone").val();
            var captcha_code = $("input#captcha_code").val();
            var message = $("textarea#message").val();
            var firstName = name; // For Success/Failure Message
            // Check for white space in name for Success/Fail message
            if (firstName.indexOf(' ') >= 0) {
                firstName = name.split(' ').slice(0, -1).join(' ');
            }
            $.ajax({
                url: "././mail/contact_me.php",
                type: "POST",
                dataType: 'json',
                data: {
                    name: name,
                    phone: phone,
                    email: email,
                    captcha_code: captcha_code,
                    message: message
                },
                cache: false,
                success: function(response) {
                    if (response.error) {

                    $('#success').html("<div class='alert alert-danger'>");
                    $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-danger').append("<strong>The security code enstered was incorrect.");
                    $('#success > .alert-danger').append('</div>');
                    } else {
                    // Enable button & show success message
                    $("#btnSubmit").attr("disabled", false);
                    $('#success').html("<div class='alert alert-success'>");
                    $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-success')
                        .append("<strong>Your message has been sent. </strong>");
                    $('#success > .alert-success')
                        .append('</div>');

                    //clear all fields
                    $('#contactForm').trigger("reset");
                        }
                },
                error: function() {
                    // Fail message
                    $('#success').html("<div class='alert alert-danger'>");
                    $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
                    $('#success > .alert-danger').append('</div>');
                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
            })
        },
        filter: function() {
            return $(this).is(":visible");
        },
    });

    $("a[data-toggle=\"tab\"]").click(function(e) {
        e.preventDefault();
        $(this).tab("show");
    });
});

// When clicking on Full hide fail/success boxes
$('#name').focus(function() {
    $('#success').html('');
});

1 个答案:

答案 0 :(得分:0)

如果它只发送到默认的电子邮件地址,我认为原因在于:

switch ($_POST['deptid']) {
case "church":
    $to = "church@domain.com";
    break;
case "school":
    $to = "school@domain.com";
    break;
case "preschool":
    $to = "preschool@domain.com";
    break;
default:
    $to = "info@domain.com";
    break;
}

尝试var_dump $ _POST ['deptid']变量,并检查它是否包含你想要的。