从表单中获取可选输入值?

时间:2016-11-17 15:27:38

标签: javascript php html forms twitter-bootstrap

我已经为自己的用法修改了一个bootstrap表单。此电子邮件表格收集姓名,电子邮件,电话和电子邮件。问题输入。用户点击发送按钮,它将信息通过电子邮件发送到预定的电子邮件地址。

我希望 可选 能够捕获用户的电话号码。我删除了required& data-validation-required-message个标记并添加了name="phone"

现在,以下输入只会将电子邮件发送到预定地址 IF 手机输入字段中有一个电话号码...我想要它无论电话号码是否发送电子邮件。

我在俯瞰什么?

HTML联系表格:

<div class="col-xs-12">
<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" name="phone">
    <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>Question? We've got answers!</label>
    <textarea rows="5" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Oops, tell us what's on your mind."></textarea>
    <p class="help-block text-danger"></p>
     </div>
     </div>
    <br>
    <div id="success"></div>

     <div class="row">
     <div class="input-group">
     <div class="form-group col-xs-6">
     <button type="submit" class="btn btn-contact btn-sm hover">SEND</button>

JS:

$(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 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",
                data: {
                    name: name,
                    phone: phone,
                    email: email,
                    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'>&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('');
});

PHP:

<?php
// Check for empty fields
if(empty($_POST['name'])        ||
   empty($_POST['email'])       ||
   empty($_POST['phone'])       ||
   empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No arguments Provided!";
    return false;
   }

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

// Create the email and send the message
$to = 'info@example.com'; 
$email_subject = "Website Contact Form:  $name";
$email_body = "Hey!! You have received a new message from your 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: info@example.com\n"; 
$headers .= "Reply-To: $email_address"; 
mail($to,$email_subject,$email_body,$headers);
return true;            
?>

0 个答案:

没有答案