I have a form and I want it to be send to my email. This is my HTML script with the form:
<form id="contact-form" class="contact-form style-2">
<div class="form-row">
<div class="form-col-2">
<input type="text" name="cf-name" placeholder="Naam*">
</div>
<div class="form-col-2">
<input type="text" name="cf-email" placeholder="Email*">
</div>
</div>
<div class="form-row">
<div class="form-col-2">
<input type="tel" name="cf-phone" placeholder="Telefoonnummer">
</div>
<div class="form-row">
<div class="form-col">
<textarea name="cf-message" rows="2" placeholder="Vragen/verzoeken"></textarea>
</div>
</div>
<button type="submit" class="btn style-3" data-type="submit">Verzend</button>
<p class="text-size-small2">Velden met een * zijn vereist.</p>
</form>
I want to make use of AJAX and this is my script.
if ($('#contact-form').length) {
var cf = $('#contact-form');
cf.append('<div class="message-container"></div>');
cf.on("submit", function (event) {
var self = $(this),
text;
var request = $.ajax({
url: "bat/mail.php",
type: "post",
data: self.serialize()
});
request.then(function (data) {
if (data == "1") {
text = "Your message has been sent successfully!";
cf.find('input:not([type="submit"]),textarea').val('');
$('.message-container').html('<div class="alert-box success"><i class="icon-smile"></i><p>' + text + '</p></div>')
.delay(150)
.slideDown(300)
.delay(4000)
.slideUp(300, function () {
$(this).html("");
});
} else {
if (cf.find('textarea').val().length < 10) {
text = "Message must contain at least 10 characters!"
}
if (cf.find('input').val() == "") {
text = "All required fields must be filled!";
}
$('.message-container').html('<div class="alert-box error"><i class="icon-warning"></i><p>' + text + '</p></div>')
.delay(150)
.slideDown(300)
.delay(4000)
.slideUp(300, function () {
$(this).html("");
});
}
}, function () {
$('.message-container').html('<div class="alert-box error"><i class="icon-warning"></i><p>Connection to server failed!</p></div>')
.delay(150)
.slideDown(300)
.delay(4000)
.slideUp(300, function () {
$(this).html("");
});
});
event.preventDefault();
});
}
And this is my PHP file:
<?php
$user_email = "fr.sven.fr@hotmail.com";
$mail = array(
"name" => htmlspecialchars($_POST['cf-name']),
"email" => htmlspecialchars($_POST['cf-email']),
"subject" => htmlspecialchars($_POST['cf-subject']),
"message" => htmlspecialchars($_POST['cf-message'])
);
function validate($arr){
return !empty($arr['name']) && strlen($arr['message']) > 20 && filter_var($arr['email'],FILTER_VALIDATE_EMAIL);
}
if(validate($mail)){
echo mail($user_email, $mail['subject'],
"Name : {$mail['name']}\n"
."E-mail : {$mail['email']}\n"
."Message : {$mail['message']}"
);
}
?>
When I submit the file without typing filling in the form I get the correct error. When I fill in the message with less than 10 characters, I get the correct error, but when I fill in everything correctly I get the error:
Notice: Undefined index: cf-name in /Applications/MAMP/htdocs/Klus spanje/php/mail.php on line 7
Notice: Undefined index: cf-email in /Applications/MAMP/htdocs/Klus spanje/php/mail.php on line 8
Notice: Undefined index: cf-subject in /Applications/MAMP/htdocs/Klus spanje/php/bat/mail.php on line 9
Notice: Undefined index: cf-message in /Applications/MAMP/htdocs/Klus spanje/php/bat/mail.php on line 10
I have no idea what I do wrong, can someone help?