如何使用联系表单发送selectpicker数据

时间:2017-01-27 22:55:10

标签: javascript php html forms twitter-bootstrap

我试图让我的联系表单从选择菜单中发送所选选项。所有输入数据都在发送(姓名,电话,电子邮件,郊区,邮政编码),但不是选择选项(状态)。我也尝试过更改选择课程,但这并没有奏效。

编辑:我尝试过更改:

var state = $(".selectpicker#state").val(); 

为:

var state = $("select#state").val(); 

哪个没有工作

这是HTML:

<form name="sentMessage" id="contactForm" novalidate>
                        <div class="row">
                            <div class="col-md-6">

                               <div class="form-group">
                                    <input type="name" class="form-control" placeholder="Your Name *" name="name" id="name" required data-validation-required-message="Please enter your name.">
                                    <p class="help-block text-danger"></p>
                                </div>

                                <div class="form-group">
                                    <input type="phone" class="form-control" placeholder="Your Phone Number *" name="phone" id="phone" required data-validation-required-message="Please enter your phone number.">
                                    <p class="help-block text-danger"></p>
                                </div>

                                <div class="form-group">
                                    <input type="email" class="form-control" placeholder="Your Email *" name="email" id="email" required data-validation-required-message="Please enter your email address.">
                                    <p class="help-block text-danger"></p>
                                </div>

                                   <div class="form-group">
                                    <input type="text" class="form-control" name="suburb" placeholder="Your Suburb *" id="suburb" required data-validation-required-message="Please enter your address.">
                                    <p class="help-block text-danger"></p>
                                </div> 

                              <div class="select"> 
                               <div class="col-sm-6">
                                <div class="form-group">
                    <select class="selectpicker" name="state" id="state" data-width="100%" data-height="100%" title="Choose State *" data-style="btn-primary">               
                        <option value="QLD">QLD</option>
                        <option value="NSW">NSW</option>
                </select>
                </div>    
                   </div>
                  </div>

                               <div class="right-form-column">
                                <div class="col-sm-6">
                                 <div class="form-group">
                                    <input type="number" class="form-control" name="postcode" placeholder="Post Code *" id="postcode" required data-validation-required-message="Please enter your post code.">
                                    <p class="help-block text-danger"></p>
                                 </div>                                              
                                </div>
                   </div>                                                                                           
                  </div>
</form>

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 suburb = $("input#suburb").val();
            var state = $(".selectpicker#state").val();
            var postcode = $("input#postcode").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: "GET",
                data: {
                    name: name,
                    phone: phone,
                    email: email,
                    suburb: suburb,
                    state: state,
                    postcode: postcode,
                    message: message
                },
                cache: false,
                success: function() {
                    // 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'>×")
                        .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'>×")
                        .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('');
});

PHP

<?php
// check if fields passed are empty
if(empty($_GET['name'])        ||
   empty($_GET['phone'])       ||
   empty($_GET['email'])       ||
   empty($_GET['suburb'])      ||
   empty($_GET['state'])      ||
   empty($_GET['postcode'])      ||
   empty($_GET['message']) ||
   !filter_var($_GET['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No arguments Provided!";
    return false;
   }

$name = $_GET['name'];
$phone = $_GET['phone'];
$email_address = $_GET['email'];
$suburb = $_GET['suburb'];
$state = $_GET['state'];
$postcode = $_GET['postcode'];
$message = $_GET['message'];

// create email body and send it    
$to = 'myname@mydomain.com'; // PUT YOUR EMAIL ADDRESS HERE
$email_subject = "P Services Contact Form:  $name"; // EDIT THE EMAIL SUBJECT LINE HERE
$email_body = "You have received a new message. \n\n".                 
                   " Here are the details:\n \nName: $name \n ".                  
                   "Email: $email_address\n Suburb: $suburb\n State: $state\n Post Code: $postcode\n Message \n $message";
$headers = "From: myname@mydomain.com\n";
$headers .= "Reply-To: $email_address"; 
if(mail($to,$email_subject,$email_body,$headers)){ 
echo "Mail sent successfully.";
} 
else{ echo "Error.";
}        
?>

2 个答案:

答案 0 :(得分:0)

尝试更改

var state = $(".selectpicker#state").val();

为:

var state = $("select#state").val();

答案 1 :(得分:0)

如果您想获取所选选项的值,请尝试此操作。

var state = document.getElementById("state");
var selectedUser= state.options[state.selectedIndex].value;
祝你好运。