Not sure if anyone will be able to answer this.
I have just created a PHP Ajax form on my site which is validated with jQuery. All fields are validated so that they need content in order for the form to be submitted.
For some reason I keep receiving emails sent from the form but they don't contain any form information. Has anyone any idea why this may be? See code and screenshots below (I have removed the jQuery validation code to save space but have tested it and it is working) -
---HTML---
<div class="col-md-6">
<div class="thankyou-message">
<p class="hero">THANK YOU!!!</p>
<p>For submitting your registration.</p>
<p>We have received your information and will contact you in due course.</p>
</div>
<div class="registration-form my-5 my-md-0">
<div class="row">
<div class="col-sm-4">
<label for="ageteam">Age group and team</label>
</div>
<div class="col-sm-8">
<input class="w-100" type="text" id="age-team" name="testage" required />
<span class="error age-team">Please tell us your age group</span>
</div>
</div>
<div class="row margin-top--20 margin-bottom--16 border-bottom--orange">
<div class="col-12">
PLAYER
</div>
</div>
<div class="row margin-bottom--8">
<div class="col-sm-4">
<label for="name">Name</label>
</div>
<div class="col-sm-8">
<input class="w-100" class="w-100" type="text" id="name" name="name" required />
<span class="error name">Please tell us your name</span>
</div>
</div>
<div class="row margin-bottom--8">
<div class="col-sm-4">
<label for="address">Address</label>
</div>
<div class="col-sm-8">
<textarea class="w-100" id="address" name="address" rows="4" required></textarea>
<span class="error address">Please tell us your address</span>
</div>
</div>
<div class="row margin-bottom--8">
<div class="col-sm-4">
<label for="postcode">Postcode</label>
</div>
<div class="col-sm-8">
<input class="w-100" type="text" id="postcode" name="postcode" required />
<span class="error postcode">Please tell us your postcode</span>
</div>
</div>
<div class="row margin-bottom--8">
<div class="col-sm-4">
<label for="dob">Date of birth</label>
</div>
<div class="col-sm-8">
<input class="w-100" type="text" id="dob" name="dob" required />
<span class="error dob">Please tell us your date of birth</span>
</div>
</div>
<div class="row margin-bottom--8">
<div class="col-sm-4">
<label for="school">School</label>
</div>
<div class="col-sm-8">
<input class="w-100" type="text" id="school" name="school" required />
<span class="error school">Please tell us the name of your school</span>
</div>
</div>
<div class="row margin-top--20 margin-bottom--16 border-bottom--orange">
<div class="col-12">
PARENT OR GUARDIAN
</div>
</div>
<div class="row margin-bottom--8">
<div class="col-sm-4">
<label for="parent-name">Name</label>
</div>
<div class="col-sm-8">
<input class="w-100" type="text" id="parent-name" name="parent-name" required />
<span class="error parent-name">Please tell us the name of your parent or guardian</span>
</div>
</div>
<div class="row margin-bottom--8">
<div class="col-sm-4">
<label for="parent-address">Address</label>
</div>
<div class="col-sm-8">
<textarea class="w-100" id="parent-address" name="parent-address" rows="4" required></textarea>
<span class="error parent-address">Please tell us your parent or guardians address</span>
</div>
</div>
<div class="row margin-bottom--8">
<div class="col-sm-4">
<label for="parent-postcode">Postcode</label>
</div>
<div class="col-sm-8">
<input class="w-100" type="text" id="parent-postcode" name="parent-postcode" required />
<span class="error parent-postcode">Please tell us the postcode of your parent or guardian</span>
</div>
</div>
<div class="row margin-bottom--8">
<div class="col-sm-4">
<label for="phone">Phone</label>
</div>
<div class="col-sm-8">
<input class="w-100" type="text" id="phone" name="phone" required />
<span class="error number">Please tell us your phone number</span>
<span class="error number2">Please enter a valid phone number</span>
</div>
</div>
<div class="row margin-bottom--8">
<div class="col-sm-4">
<label for="email">Email</label>
</div>
<div class="col-sm-8">
<input class="w-100" type="text" id="email" name="testemail" required />
<span class="error email">Please tell us your email address</span>
<span class="error email2">Please enter a valid email address</span>
</div>
</div>
<div class="submit-btn">
Submit registration
</div>
</div>
</div>
---jQuery Submission Code---
$('div.submit-btn').click(function() {
if (ageTeamValidate === true && nameValidate === true && addressValidate === true && postcodeValidate === true && dobValidate === true && schoolValidate === true && parentNameValidate === true && parentAddressValidate === true && parentPostcodeValidate === true && numberValidate === true && emailValidate === true) {
console.log("form submitted");
var varData = 'ageTeam= ' ageTeam ' & name= ' name ' & address= ' address ' & postcode= ' postcode ' & dob= ' dob ' & school= ' school ' & parentName= ' parentName ' & parentAddress= ' parentAddress ' & parentPostcode= ' parentPostcode ' & number= ' number ' & email= ' email;
console.log(varData);
$.ajax({
type: "POST",
url:'form-process.php',
data: varData,
cache: false,
success: function() {
//alert("Post successful");
$('div.registration-form').css('display', 'none');
$('div.thankyou-message').css('display', 'block');
}
});
}
return false;
});
--- PHP form processing code ---
<?php
$ageTeam = $_POST['ageTeam'];
$name = $_POST['name'];
$address = $_POST['address'];
$postcode = $_POST['postcode'];
$dob = $_POST['dob'];
$school = $_POST['school'];
$parentName = $_POST['parentName'];
$parentAddress = $_POST['parentAddress'];
$parentPostcode = $_POST['parentPostcode'];
$number = $_POST['number'];
$email = $_POST['email'];
$email_thankyou = "
Hi".$name."
Thank you for registering.
We look forward to welcoming you to our team and will contact you in due course.
Thanks again
";
$email_alert = "
".$name."has completed a registration form.
Their details are listed below:
Age and team:".$ageTeam."
Players full name:".$name."
Players address:".$address."
Players postcode:".$postcode."
Players date of birth:".$dob."
Players school:".$school."
Parent/guardians name:".$parentName."
Parent/guardians address:".$parentAddress."
Parent/guardians postcode:".$parentPostcode."
Contact number:".$number."
Email Address:".$email."
";
$headers = "From: Test PHP Ajax Form";
// Notification email to client
mail($email, 'Thank you for registering', $email_thankyou, $headers);
// Notification email to Matthiola
mail('[email protected]', 'Player Registration', $email_alert, $headers);
?>
--- Email received ---
Received email shows static content but no information that should be provided by user.
CodePudding user response:
Most probably this is because of the bots and web crawlers going through your site. You should validate the inputs from the server side(using PHP) as well before sending the email.
if ($name != '' && $address != ''){
// Notification email to client
mail($email, 'Thank you for registering', $email_thankyou, $headers);
// Notification email to Matthiola
mail('[email protected]', 'Player Registration', $email_alert, $headers);
}