I just come arround the corner with AJAX calls and trying to learn it. I have a form i am trying to push through an AJAX call, but getting a constant error it can't send, and I cant wrap my head arround it why it doesn't work.
Form:
<form class="form-horizontal" id="contact-form" method="post">
<div class="control-group">
<div class="controls">
<input type="text" name="name" id="name" placeholder="Naam" class="form-control input-lg">
</div>
</div>
<div class="control-group">
<div class="controls">
<input type="text" name="email" id="email" placeholder="Email adres" class="form-control input-lg">
</div>
</div>
<div class="control-group">
<div class="controls">
<textarea name="message" id="message" rows="8" class="form-control input-lg" placeholder="Bericht"></textarea>
</div>
</div>
<div class="form-actions">
<button type="submit" id="submit" class="btn btn-default btn-lg btn-block">Verstuur Bericht</button>
</div>
<div id="succes">
</div>
</form>
The Ajax Call
$('form').bind('submit',function(){
$.ajax({
type : 'POST',
url : 'inc/actions/sendmail.php',
data : $(this).serialize(),
cache : false,
dataType: 'text',
success : function (serverResponse) { alert('mail sent successfully.'); },
error : function (jqXHR, textStatus, errorThrown) {alert('error sending mail');}
});
})
And the sendmail.php
$name = mysqli_real_escape_string($con,$_POST["naam"]);
$from = mysqli_real_escape_string($con,$_POST["email"]);
$message = mysqli_real_escape_string($con,$_POST["bericht"]);
$message = wordwrap($message, 70);
$to = "example@myemail.nl";
$subject = "Een gebruiker heeft een vraag of opmerking";
$body = "Some messages and colloring, to much to paste cause it's inline-markup";
$headers = "MIME-Version: 1.0" . "
";
$headers .= "Content-type:text/html;charset=UTF-8" . "
";
$headers .= "From: noreply@ajaxfailure.nl";
if ($name == ''){
echo '<p>U heeft geen naam ingevuld</p>';
}else if($from == ''){
echo '<p>U heeft geen emailadres achter gelaten</p>';
}else if($body == ''){
echo '<p>U heeft geen bericht ingevuld niet ingevuld';
}else {
if (mail ($to, $subject, $body, $headers)) {
echo '<p>Uw bericht is verzonden.</p>';
} else {
echo '<p>Er is iets mis gegaan bij het verzenden van de data, probeer het opnieuw.</p>';
}
}
Everytime it returns me the error alert from the AJAX call and doesnt execute anything. My console doesn't spot an error either and remains competely blank.
I checked the console as per suggestion on the network tab and it doesnt even get on the page itself. There is no 404 error on that part. It seems the request gets canceled.