Código HTML :
<html> <head> </head> <body> <div class="col-md-12"> <form name="sentMessage" id="contactForm" autocomplete="off" novalidate> <div class="control-group col-md-6 no-padding-left no-padding"> <div class="controls"> <input type="text" class="form-control" placeholder="Nombre" id="name" required data-validation-required-message="Por favor, escriba su nombre" /> <p class="help-block"></p> </div> </div> <div class="control-group col-md-6 no-padding-right no-padding"> <div class="controls"> <input type="email" class="form-control" placeholder="E-mail" id="email" required data-validation-required-message="Por favor, escriba su correo" /> </div> </div> <div class="control-group"> <div class="controls"> <textarea rows="10" cols="100" class="form-control" placeholder="Mensaje" id="message" required data-validation-required-message="Introduzca su mensaje" minlength="5" data-validation-minlength-message="Min 5 caracteres" maxlength="999" style="resize:none"></textarea> </div> </div> <div id="success"> </div> <!-- For success/fail messages --> <button type="submit" class="enviar"></button><br/> </form> <div class="col-md-12 text-center"> <a class="social" href="http://www.twitter.com/vulpdg"> <span class="icon-social-twitter-circular"></span> </a> <a class="social" href="http://www.facebook.com/vulpdg"> <span class="icon-social-facebook-circular"></span> </a> </div> </div> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="assets/js/jqBootstrapValidation.js"></script> <script src="assets/js/contact_me.js"></script> </body> </html>
Código Javascript :
/* Jquery Validation using jqBootstrapValidation example is taken from jqBootstrapValidation docs */ $(function() { $("input,textarea").jqBootstrapValidation( { preventSubmit: true, submitError: function($form, event, errors) { // something to have when submit produces an error ? // Not decided if I need it yet }, submitSuccess: function($form, event) { event.preventDefault(); // prevent default submit behaviour // get values from FORM var name = $("input#name").val(); var email = $("input#email").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: "contact_me.php", type: "POST", data: {name: name, email: email, message: message}, cache: false, success: function() { // Success message $('#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>Tu mensaje ha sido enviado.</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>Disculpa "+firstName+" Parece que mi servidor de correo no responde... </ strong> ¿Podría usted por favor envíarme un correo electrónico directamente a <a href='mailto:[email protected]?Subject=Message_Me de vulpdg.cl'> [email protected] </a>? ¡Lo siento por los inconvenientes ocasionados!"); $('#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(''); });
Código PHP :
<?php // check if fields passed are empty if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message']) || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) { echo "No arguments Provided!"; return false; } $name = $_POST['name']; $email_address = $_POST['email']; $message = $_POST['message']; // create email body and send it $to = '[email protected]'; // put your email $email_subject = "Contact form submitted by: $name"; $email_body = "You have received a new message. \n\n". " Here are the details:\n \nName: $name \n ". "Email: $email_address\n Message \n $message"; $headers = "From: [email protected]\n"; $headers .= "Reply-To: $email_address"; mail($to,$email_subject,$email_body,$headers); return true; ?>