I'm using the following jQuery and PHP where people fill in a form, jQuery then goes through the fields, validates there is content and if everything is filled in correctly it will send to PHP which then formats the email and sends.
The problem is when I am trialing it, it works fine but I have received emails with the line headings such as Name:
and Email:
etc but the user input content is blank.
Could it be possible that search engines are crawling the site and come across the PHP file and this is why it sends blank data?
jQuery
$('body').on('click','.sendmail',function(){
var arr = [];
$('.form input').each(function(index, element) {
var i = $(this).prop('id');
var v = $(this).val();
var obj = {key: i, val: v};
arr.push(obj);
});
arr.push({key:"subject", val: $('.subject .active').text()});
arr.push({key:"comments", val: $('#comments').val()});
var param = {};
$.each(arr, function() {
param[this.key] = this.val;
});
var sValid;
var isValid = !$('#name, #company, #tel, #comments').filter(function() {return !this.value;}).length;
var eValid = validateEmail($('#email').val());
if ($('.subject span').hasClass("active")) {
sValid = true;
} else {
sValid = false;
}
if (isValid && sValid && eValid){
$.ajax({
type: "POST",
url: "/assets/inc/contact.php",
data: param,
dataType: "json",
success: function(data) {
$('.form span').removeClass('active');
$('.form-response').removeClass('green');
$('.form-response').removeClass('red');
if (data.sent == 1){
$('.form-response h1').text('Form is sent! We\'ll be in touch soon');
$('.form-response').addClass('green');
} else {
$('.form-response h1').text('Form not sent! Please check all fields are filled in.');
$('.form-response').addClass('red');
}
$('.form-response').slideDown().delay(3000).slideUp();
$('.form input, .form textarea').val('');
$('.form label').css({'opacity':1, 'display':'block'});
$('.form span').removeClass('active');
}
});
} else {
$('.form-response h1').text('Form not sent! Please check all fields are filled in.');
$('.form-response').addClass('red');
$('.form-response').slideDown().delay(3000).slideUp();
}
});
PHP
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
$tel = $_POST['tel'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];
$to = 'info@domain.com';
$subject = $subject;
$message = '
Name: '.$name.'<br>
Email: '.$email.'<br>
Company: '.$company.'<br>
Tel: '.$tel.'<br><br><hr><br><br>
Comments: '.$comments.'<br>
';
$headers = 'From: noreply@domain.com' . "
" .
'X-Mailer: PHP/' . phpversion();
$headers .= 'Content-Type: text/html; charset=utf-8';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "
";
$headers .= 'Content-type: text/html; charset=utf-8' . "
";
// Additional headers
$headers .= 'From: '.$name.' <'.$email.'>' . "
";
$sent = mail($to, $subject, $message, $headers);
if($sent){
$sent = 1;
} else {
$sent = 0;
}
$post_data = json_encode(array('sent' => $sent));
echo $post_data;