如何将下拉列表值发送到电子邮件?

时间:2018-08-01 10:25:27

标签: javascript php html

我有一个问题,我需要将选定的下拉列表值发送到电子邮件。我已经将联系人表格中的其他输入值发送到了电子邮件。但是我不知道如何将下拉列表值发送到电子邮件。特此附上html,php和js代码。谁能帮助我?

这是html代码

<!-- Contact form -->

 <div class="modal fade" id="modalForm" role="dialog" style="width: 100%;">
    <div class="modal-dialog">
        <div class="modal-content">
            <!-- Modal Header -->
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">&times;</span>
                    <span class="sr-only">Close</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">Contact Form</h4>
            </div>

            <!-- Modal Body -->
            <div class="modal-body">
                <p class="statusMsg"></p>
                <form role="form">
                    <div class="form-group">
                        <label for="inputName">Name</label>
                        <input type="text" class="form-control" id="inputName" placeholder="Enter your name" style="border: 1px solid #fff; border-bottom: 1px solid #ccc;"/>
                    </div>
                    <div class="form-group">
                        <label for="inputEmail">Email</label>
                        <input type="email" class="form-control" id="inputEmail" placeholder="Enter your email"/>
                    </div>
                    <div class="form-group">
                        <label for="inputMessage">Message</label>
                        <textarea class="form-control" id="inputMessage" placeholder="Enter your message"></textarea>
                    </div>
                    <div class="form-group">
                        <label for="inputService">Service</label>
                        <select name="subject" class="form-control"
                        id="inputService" size="1">
                        <option value="Option1">Pr-Matrimonial Services</option>
                        <option value="Option2">Extra Marital Affairs</option>
                        <option value="Option3">Divorce Case Support</option>
                        </select>
                    </div>
                </form>
            </div>

            <!-- Modal Footer -->
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary submitBtn" onclick="submitContactForm()">SUBMIT</button>
            </div>
        </div>
    </div>
</div>

JS代码...

<script>
function submitContactForm(){
    var usernameRegex = /^[a-zA-Z0-9]+$/;
    var reg = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
    var name = $('#inputName').val();
    var email = $('#inputEmail').val();
    var message = $('#inputMessage').val();
    if(name.trim() == '' ){
        alert('Please enter your name.');
        $('#inputName').focus();
        return false;
    }else if(name.trim() != '' && !usernameRegex.test(name)){
        alert('Please enter valid name.');
        $('#inputName').focus();
        return false;
    }else if(email.trim() == '' ){
        alert('Please enter your email.');
        $('#inputEmail').focus();
        return false;
    }else if(email.trim() != '' && !reg.test(email)){
        alert('Please enter valid email.');
        $('#inputEmail').focus();
        return false;
    }else if(message.trim() == '' ){
        alert('Please enter your message.');
        $('#inputMessage').focus();
        return false;
    }else{
        $.ajax({
            type:'POST',
            url:'contact.php',
            data:'contactFrmSubmit=1&name='+name+'&email='+email+'&message='+message,
            beforeSend: function () {
                $('.submitBtn').attr("disabled","disabled");
                $('.modal-body').css('opacity', '.5');
            },
            success:function(msg){
                if(msg == 'ok'){
                    $('#inputName').val('');
                    $('#inputEmail').val('');
                    $('#inputMessage').val('');
                    $('.statusMsg').html('<span style="color:green;">Thanks for contacting us, we\'ll get back to you soon.</p>');
                }else{
                    $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
                }
                $('.submitBtn').removeAttr("disabled");
                $('.modal-body').css('opacity', '');
            }
        });
    }
}
</script>

php代码...

<?php

if(isset($_POST['contactFrmSubmit']) && !empty($_POST['name']) && !empty($_POST['email']) && (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) && !empty($_POST['message'])){


    // Submitted form data
    $name   = $_POST['name'];
    $email  = $_POST['email'];
    $message= $_POST['message'];
    $option= $_POST["subject"];

    /*
     * Send email to admin
     */
    $to     = 'support@ribelz.net';
    $subject= 'Contact Request of privateeyelk.com';

    $htmlContent = '
    <h4>Contact request from : '.$email.'</h4>

    <p>Name: '.$name.'</p>
    <p>Email: '.$email.'</p>
    <p>Message: '.$message.'</p>
    <p>Service: '. $option.'</p>
    ';

    // Set content-type header for sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

    // Additional headers
    $headers .= 'From: Private Eye<privateeyelk.com>' . "\r\n";

    // Send email
    if(mail($to,$subject,$htmlContent,$headers)){
        $status = 'ok';
    }else{
        $status = 'err';
    }

    // Output status
    echo $status;die;

    }

1 个答案:

答案 0 :(得分:0)

您没有在Js代码中检索select的值

尝试在您的SubmitContactForm函数中添加此行

var subject = $('#inputService').val(); 

请选中此行

data:'contactFrmSubmit=1&name='+name+'&email='+email+'&message='+message+'&service='+subject,