My Ajax script is preventing my form from being sent. I want my Ajax script to send my form, without my page has to refresh. If i am using my form and PHP WITHOUT the Ajax script, it's sending the form to my email just fine. But when i add the AJAX to PREVENT it from refreshing, the Ajax script runs, and returns that it has been send, without being sent, and this is my question.
Why is my AJAX script blocking the form from being sent?
HTML:
<form id="KontaktForm" action="e_Sender.php" method="post">
Fulde navn * <input type="text" class="form-control" id="FuldeNavm" type="text" name="fuldt_navn"><br>
Email * <input type="email" class="form-control" id="Email" name="email"><br>
Mobil * <input type="number" class="form-control" id="Mobil" name="mobil"><br>
Antal * <input class="form-control" id="Antal" type="number" name="antal"><br>
Besked *<br><textarea class="form-control" id="Besked" rows="5" name="message" cols="30"></textarea><br>
<input class="btn btn-info" id="Submit" type="submit" name="submit" value="Submit">
</form>
AJAX:
$(function () {
$("#KontaktForm").on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'e_Sender.php',
data: $('form').serialize(),
success: function () {
alert('form was submitted');
}
});
});
});
PHP:
<?php
if(isset($_POST['submit'])){
$to = "vejby3824@gmail.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$fuldt_navn = $_POST['fuldt_navn'];
$mobil = $_POST['mobil'];
$antal = $_POST['antal'];
$subject = $fuldt_navn . " Efterspørger et tilbud!";
$message = $fuldt_navn . "\n" . "Mobil Nummer: " . $mobil . "\n\n" . "Kommer med: " . $antal . "\n\n" . "De har skrevet følgende besked: " . "\n" . $_POST['message'];
mail($to,$subject,$message);
$url = 'Index.html';
header('Location: ' . $url);
exit;
}
?>