Good morning everyone as an attempt to re-learn web programming I am trying to make a simple mailing Form in a self contained html file, so no include, no outside reference using HTML php and Ajax. The point is to make a stand alone html that won't refresh when submitting the form.
I have the html form as follow:
<form class="form2" action="" method="POST" name="myForm" id="myForm">
<div class="formtitle">Inscrivez-vous à notre infolettre</div>
<div class="input">
<div class="inputtext">Nom: </div>
<div class="inputcontent">
<input type="text" name="name" id="name" />
</div>
</div>
<div class="input">
<div class="inputtext">Courriel: </div>
<div class="inputcontent">
<input type="text" name="email" id="email" />
</div>
</div>
<div class="buttons"> <span id="error" style="display:none; color:#F00">Some Error!Please Fill form Properly </span> <span id="success" style="display:none; color:#0C0">All the records are submitted!</span>
<input class="greybutton" type="submit" value="Inscrivez-moi" />
</div>
</form>
Then I have my Ajax listen for the submit button clic:
<script>
$(document).ready(function(){
$('#myForm').on('submit',function(e) {
$.ajax({
data:$(this).serialize(),
type:'POST',
success:function(data){
console.log(data);
$("#success").show().fadeOut(5000); //=== Show Success Message==
},
error:function(data){
$("#error").show().fadeOut(5000); //===Show Error Message====
}
});
e.preventDefault(); //=== To Avoid Page Refresh and Fire the Event "Click"===
});
});
</script>
and finally I have the typical mail php code:
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = "Veuillez m'ajouter à votre newsletter";
$formcontent="From: $name \n Message: $message";
$recipient = "[email protected]";
$subject = "Newsletter";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>
For some reason it doesn't seem to ever reach the php as no mail are sent or success/error message. I doubled and tripled checked all the arguments that are passed and everything seem in order.
Thank you for your time and for sharing your knowledge!