javascript - Getting Optional input values from a form? -
i have modified bootstrap form own usage. email form collects name, email, phone & question inputs. user hits send button , emails information predetermined email address.
i'd have ability optionally capture user's phone number. i've removed required
& data-validation-required-message
tags , added name="phone"
.
right below inputs send email predetermined address if there phone number in phone input field... i'd send email regardless of phone number or not.
what overlooking?
html contact form :
<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 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 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 what's on 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 , default submit behaviour $("#btnsubmit").attr("disabled", true); event.preventdefault(); // values form var name = $("input#name").val(); var email = $("input#email").val(); var phone = $("input#phone").val(); var message = $("textarea#message").val(); var firstname = name; // success/failure message // check white space in name 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'>×") .append("</button>"); $('#success > .alert-success') .append("<strong>your message has been sent. </strong>"); $('#success > .alert-success') .append('</div>'); //clear 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 + ", seems mail server not responding. please try again later!"); $('#success > .alert-danger').append('</div>'); //clear 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 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 email , send message $to = 'info@example.com'; $email_subject = "website contact form: $name"; $email_body = "hey!! have received new message website contact form.\n\n"."here 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; ?>
Comments
Post a Comment