I have this email code from a website template that I want to use for my site. The problem is that when I click on the "Send message" button, the form simply refreshes and nothing is sent. If there is any text data in the fields, it simply disappears.
I have looked at foums for a solution but to no avail. What am I doing wrong?
Form Html
<form name="sentMessage" id="contactForm" method='post' novalidate action="public_html/bin/contact_me.php">
<div class="control-group form-group">
<div class="controls">
<label>Full Name</label>
<input type="text" class="form-control" name="name" id="name" placeholder="Full Name" required data-validation-required-message="Please enter your name.">
<p class="help-block"></p>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Email Address</label>
<input type="email" class="form-control" name="email" id="email" placeholder="Email Address" required data-validation-required-message="Please enter your email address.">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Phone Number</label>
<input type="tel" class="form-control" name="phone" id="phone" placeholder="Phone Number" required data-validation-required-message="Please enter your phone number.">
</div>
</div>
</div>
<div class="form-group">
<label>Subject</label>
<input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" required data-validation-required-message="Please enter your phone number.">
</div>
<div class="form-group">
<label>Message</label>
<textarea rows="10" cols="100" class="form-control" name="message" id="message" placeholder="Please Enter Your Message Here..." required data-validation-required-message="Please enter your message" maxlength="999" style="height:150px;"></textarea>
</div>
<div id="success"></div>
<div>
<button type="submit" class="btn btn-two">Send message</button>
<p><br/></p>
</div>
</form>
PHP Code
<?php
// check if fields passed are empty
if(empty($_POST['name']) ||
empty($_POST['phone']) ||
empty($_POST['email']) ||
empty($_POST['subject']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$phone = $_POST['phone'];
$email_address = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// create email body and send it
$to = 'info@mywebsite.com'; // PUT YOUR EMAIL ADDRESS HERE
$email_subject = "$subject"; // EDIT THE EMAIL SUBJECT LINE HERE
$email_body = "You have received a new message from your website's contact form.\n\n"."Here are the details:\n\nName: $name\n\nPhone: $phone\n\nEmail: $email_address\n\nMessage:\n$message";
$headers = "From: noreply@your-domain.com\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
Js
$(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 phone = $("input#phone").val();
var email = $("input#email").val();
var subject = $("input#subject").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: "bin/contact_me.php",
type: "POST",
data: {
name: name,
phone: phone,
email: email,
subject: subject,
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>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...</strong> Could you please email me directly to <a href='mailto:me@example.com?Subject=Message_Me from myprogrammingblog.com;>me@example.com</a> ? Sorry for the inconvenience!");
$('#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('');
});
How do I solve this?