I have a bootstrap form, I try to validate it, the validation works, then post it through my php file. Like I said the validation works, but the email will not send, he's my code
/*
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 property = $("input#property").val();
var BillingAddress = $("textarea#BillingAddress").val();
var Floor = $("input#Floor").val();
var MachineType = $("input#MachineType").val();
var MachineLocation = $("textarea#MachineLocation").val();
var description = $("textarea#description").val();
var email = $("input#email").val();
var daytime = $("input#daytime").val();
var Name = Name; // For Success/Failure Message
// Check for white space in name for Success/Fail message
if (Name.indexOf(' ') >= 0) {
Name = Name.split(' ').slice(0, -1).join(' ');
}
$.ajax({
url: "./js/contact_me.php",
type: "POST",
data: {Name: name, property: property, BillingAddress: BillingAddress, Floor: Floor, MachineType: MachineType, MachineLocation: MachineLocation, description: description, email: email, daytime: daytime},
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('');
});
<form name="sentMessage" id="contactForm" novalidate>
<div class="row">
<div class="col-sm-6">
<div class="control-group">
<div class="controls">
<label for="Name">Full Name</label>
<input type="text" class="form-control"
placeholder="Full Name" id="Name" required
data-validation-required-message="Please enter your full name" />
<p class="help-block"></p>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="control-group">
<div class="controls">
<label for="property">Property Name</label>
<input type="text" class="form-control"
placeholder="Property Name" id="property" required
data-validation-required-message="Please enter the property name" />
<p class="help-block"></p>
</div>
</div>
</div>
</div><!--Close Roww-->
<div class="row">
<div class="col-sm-6">
<div class="control-group">
<div class="controls">
<label for="BillingAddress">Billing Address</label>
<textarea rows="3" cols="100" class="form-control"
id="BillingAddress" required
data-validation-required-message="Please enter your address" minlength="5"
data-validation-minlength-message="Min 5 characters"
maxlength="999" style="resize:none"></textarea>
</div>
</div>
</div>
<div class="col-sm-6" >
<div class="control-group">
<div class="controls">
<label for="apt">Apartment Number</label>
<input type="text" class="form-control"
id="apt" required
data-validation-required-message="Please enter a subject" />
<p class="help-block"></p>
</div>
</div>
</div>
</div><!--Close Row-->
<div class="row">
<div class="col-sm-6">
<div class="control-group">
<div class="controls">
<label for="floor">Floor</label>
<input type="text" class="form-control"
id="floor" required
data-validation-required-message="Please enter a subject" />
<p class="help-block"></p>
</div>
</div>
</div>
<div class="col-sm-6" >
<div class="control-group">
<div class="controls">
<label for="MachineType">Machine Type</label>
<select id="MachineType" name="MachineType" class="form-control" >
<option value="1">Select Machine</option>
<option value="2">Washer</option>
<option value="3">Dryer</option>
<option value="4">Card Machine</option>
<option value="5">Other</option>
</select>
</div>
</div>
</div>
</div><!--Close Row-->
<div class="row">
<div class="col-sm-6">
<div class="control-group">
<div class="controls">
<label for="MachineLocation">Machine Number or Location</label>
<textarea rows="3" cols="100" class="form-control"
id="MachineLocation" required
data-validation-required-message="( ie washer next to Door)" minlength="5"
data-validation-minlength-message="Min 5 characters"
maxlength="999" style="resize:none"></textarea>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="control-group">
<div class="controls">
<label for="description">Description of problem</label>
<textarea rows="3" cols="100" class="form-control"
id="description" required
data-validation-required-message="( ie Washer next to Door)" minlength="5"
data-validation-minlength-message="Min 5 characters"
maxlength="999" style="resize:none"></textarea>
</div>
</div>
</div>
</div><!--Close Row-->
<div class="row">
<div class="col-sm-6">
<div class="control-group">
<div class="controls">
<label for="email">Email</label>
<input type="email" class="form-control"
id="email" required
data-validation-required-message="Please enter your email" />
</div>
</div>
</div>
<div class="col-sm-6">
<div class="control-group">
<div class="controls">
<label for="daytime">Daytime Number</label>
<input type="text" class="form-control"
id="daytime" required
data-validation-required-message="Please enter a phone number" />
<p class="help-block"></p>
</div>
</div>
</div>
</div><!--Close Row-->
<div class="row">
<div id="success"> </div> <!-- For success/fail messages -->
<div class="col-sm-12"> <br />
<button type="submit" class="btn btn-info pull-left">Submit</button><br /></div>
</div><!--close row-->
</form>
<?php
// check if fields passed are empty
if(empty($_POST['Name']) ||
empty($_POST['property']) ||
empty($_POST['BillingAddress']) ||
empty($_POST['apt']) ||
empty($_POST['floor']) ||
empty($_POST['MachineType']) ||
empty($_POST['MachineLocation']) ||
empty($_POST['description']) ||
empty($_POST['email']) ||
empty($_POST['daytime']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$Name = $_POST['Name'];
$property = $_POST['property'];
$BillingAddress = $_POST['BillingAddress'];
$apt = $_POST['apt'];
$floor = $_POST['floor'];
$MachineType = $_POST['MachineType'];
$MachineLocation = $_POST['MachineLocation'];
$description = $_POST['description'];
$email = $_POST['email'];
$daytime = $_POST['daytime'];
// create email body and send it
$to = 'my@gmail.com'; // put your email
$email_subject = "Service Request form submitted by: $Name $property";
$email_body = "You have received a new Service Request. \n\n".
"Here are the details:\n \nName: $Name \n ".
"Porperty Name: $property \n ".
"Billing Address: $BillingAddress \n".
"Email: $email\n".
"Property: $property\n".
"Apartment Number: $apt \n".
"Floor: $floor \n".
"Machine Type: $MachineType \n".
"Machine Location: $MachineLocation \n".
"Description: $description \n".
"Daytime Phone: $daytime \n";
$headers = "From: test@mysite.com\n";
$headers .= "Reply-To: $email";
mail($to,$email_subject,$email_body,$headers);
return true;
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
?>
I had another person ask about the first line of AJAX and the path to the PHP file is correct, if it wasn't, the validation wouldn't work. I've looked at this for a couple of days now and can't find the error.
I am trying to send to my gmail account and I know my server supports sending email as I have done it one pages.