confirmform.php
<?php
$instance = new CheckForm;
$instance -> checkSubmission();
class CheckForm
{
public function checkSubmission()
{
$origEmail = $_POST['origEmail'];
$confirmEmail = strip_tags($_POST['confirmEmail']);
$name = trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(html_entity_decode(strip_tags($_POST['name']))))));
$age = $_POST['age'];
$gender = $_POST['gender'];
$country = $_POST['country'];
$catcher = strip_tags($_POST['catcher']);
$mathAnswer = strip_tags($_POST['addition']);
$rightAnswer = $_POST['mathAnswer'];
$submissionTime = $_POST['submissionTime'];
$status = 0;
$response = array("validation" => "fail", "errorMessage" => " ", "database" => " ", "name" => " ", "email" => " ");
if ($origEmail != $confirmEmail || empty($confirmEmail)) {
if (empty($name) || !ctype_alpha($name)) {
if ($country == "Select Location") {
if (!ctype_digit($age)) {
if ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: that's not a valid submission.";
} else
$response['errorMessage'] = "Error: e-mails don't match, invalid name entered, non-numeric age, and no location selected.";
} elseif ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: e-mails don't match, invalid name given, no location specified, and math answer was incorrect.";
} else
$response['errorMessage'] = "Error: e-mails don't match, invalid name specified, and no location specified.";
} elseif (!ctype_digit($age)) {
if ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: e-mails don't match, invalid name supplied, non-numeric age entered, and math answer is incorrect.";
} else
$response['errorMessage'] = "Error: e-mails don't match, invalid name supplied, and age can only be numeric.";
} elseif ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: e-mails don't match, invalid name supplied, and the math answer was incorrect.";
} else
$response['errorMessage'] = "Error: e-mails don't match and invalid name entered (letters only).";
} elseif (!ctype_digit($age)) {
if ($country == "Select Location") {
if ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: e-mails don't match, age must be numeric, no location selected, and math answer is incorrect.";
} else
$response['errorMessage'] = "Error: e-mails don't match, age must be numeric, and no location selected.";
} elseif ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: e-mails don't match, invalid (non-numeric) age supplied, and math answer is incorrect.";
} else
$response['errorMessage'] = "Error: e-mails don't match and non-numeric age was entered.";
} elseif ($country == "Select Location") {
if ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: e-mails don't match, no location selected, and math answer is incorrect.";
} else
$response['errorMessage'] = "Error: e-mails don't match and no location was selected.";
} elseif ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: e-mail addresses don't match and math answer was incorrect.";
} else
$response['errorMessage'] = "Error: e-mall addresses don't match.";
} elseif (empty($name)) {
if ($country == "Select Location") {
if (!ctype_digit($age)) {
if ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: no name supplied, non-numeric age entered, no location selected, and math answer is incorrect.";
} else
$response['errorMessage'] = "Error: no name entered, age not in numeric format, and no location selected.";
} elseif ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: no name entered, no location selected, and math answer is incorrect.";
} else
$response['errorMessage'] = "Error: no name entered and no location selected.";
} elseif (!ctype_digit($age)) {
if ($country == "Select Location") {
if ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: no name entered, non-numeric age supplied, no location selected, and math answer is incorrect.";
} else
$response['errorMessage'] = "Error: no name entered, non-numeric age supplied, and no location selected.";
} elseif ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: no name entered, non-numeric age entered, and math answer is incorrect.";
} else
$response['errorMessage'] = "Error: no name entered and non-numeric age supplied.";
} elseif ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: no name entered and math answer is incorrect.";
} else
$response['errorMessage'] = "Error: no name entered.";
} elseif (!ctype_alpha($name)) {
if ($country == "Select Location") {
if (!ctype_digit($age)) {
if ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: name can only contain letters, age must be numerical, no location selected, and math answer is incorrect.";
} else
$response['errorMessage'] = "Error: name can only contain letters, non-numeric age supplied, and no location is selected.";
} elseif ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: name can only contain letters, no location selected, and math answer is incorrect.";
} else
$response['errorMessage'] = "Error: name can only have letters and no location is selected.";
} elseif (!ctype_digit($age)) {
if ($country == "Select Location") {
if ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: name can only contain letters, age must be numeric, no location selected, and math answer is incorrect.";
} else
$response['errorMessage'] = "Error: name can only contain letters, non-numeric age supplied, and no location is selected.";
} elseif ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: name can only have letters, non-numeric age entered, and math answer is incorrect.";
} else
$response['errorMessage'] = "Error: name must have letters only and age can only be numeric.";
} elseif ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: name must only have letters and math answer is incorrect.";
} else
$response['errorMessage'] = "Error: name can only contain letters.";
} elseif (!ctype_digit($age)) {
if ($country == "Select Location") {
if ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: age can only be numbers, no location selected, and math answer is incorrect.";
} else
$response['errorMessage'] = "Error: age can only have numbers and no location is selected.";
} elseif ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: age can only contain numbers and math answer is incorrect.";
} else
$response['errorMessage'] = "Error: age can only contain numbers.";
} elseif ($country == "Select Location") {
if ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: no location is selected and math answer is incorrect.";
} else
$response['errorMessage'] = "Error: no location is selected.";
} elseif ($mathAnswer != $rightAnswer) {
$response['errorMessage'] = "Error: math answer is incorrect.";
} else
$status = 1;
if ($status == 1) {
require_once("categoryfinder.php");
$categoryFinder = new CategoryFinder;
$category = $categoryFinder -> getCategory();
$response['validation'] = "pass";
$response['name'] = $name;
$response['email'] = $confirmEmail;
require_once('databasewriter.php');
$dbWriter = new DatabaseWriter;
$dbCode = $dbWriter -> writeUserToDatabase($confirmEmail, $name, $age, $gender, $country, $category);
if ($gender == "Male")
$gender = "M";
elseif ($gender == "Female")
$gender = "F";
else
$gender = NULL;
if ($dbCode == 1) {
$response['database'] = "pass";
require_once('emailsender.php');
$emailSender = new EmailSender;
$emailSender -> sendWelcomeEmail($confirmEmail, $name);
} else {
$response['database'] = "fail";
$response['validation'] = "fail";
}
if ($dbCode == 2) {
$response['errorMessage'] = "Server error. Please try again later or <a href=\\"mailto:admin@worldreviewgroup.com\\">let us know</a> about the issue.";
} elseif ($dbCode == 3) {
$response['errorMessage'] = "That e-mail address already exists!";
}
}
echo json_encode($response);
}
}
?>
lpemailbox.js
// Emailbox and form handler script
$("#go").focus(function(){
if ($("#go").val() == $("#go").prop('defaultValue')){
$("#go").val('');
}
});
$("#go").on('focus blur', function(e) {
var v = $(this).val()
if (e.type == "focus"){
v = (v == "your e-mail")? "" : v;
} else {
v = (v == "")? "your e-mail" : v;
}
$(this).val(v);
});
//this is the function called by the success value of the first .ajax() call
function testFirstResults(response){
if (response.indexOf("Submission Successful") != -1){
$("#blackoverlay, #submissionform").fadeIn(400);
} else if (response.indexOf("Invalid E-mail") != -1){
$("#invalidemail").fadeIn(1200).delay(1400).fadeOut(1800);
}
}
function testSecondResults(data){
if (data['validation'] == "pass"){
$("#submissionform").css("display", "none");
$("#person").append(data['name']);
$("#email").append(data['email']);
$("#successfulsubmit").css("display", "block");
$("#blackoverlay, #successfulsubmit").delay(5600).fadeOut(800);
setTimeout(function(){
$(".exit").delay(2800).append("Click to Exit");
$("#blackoverlay, #successfulsubmit").click(function(e){
$("#blackoverlay, #successfulsubmit").remove();
});
}, 2800);
$("#emailbox input").each(function(){
$(this).prop("disabled", "disabled");
});
$("#emailbox :submit").css("width", "60px").val('Thanks!');
$("#go").css("width", "108px");
} else {
$("#errormessage").css("display", "block");
$("#errormessage p").empty().append(data['errorMessage']);
}
}
var mathAnswer = 0;
var startTime = 0;
var endTime = 0;
var submissionTime;
function generateEquation(){
var num1 = Math.floor(Math.random() * 5) + 1;
var num2 = Math.floor(Math.random() * 5) + 1;
mathAnswer = num1 + num2;
$("#math").append("What is " + num1 + " + " + num2 + "?");
}
$(document).ready(function(){
generateEquation();
$("#emailbox").submit(function(e){
startTime = jQuery.now();
var email = $("#go").val();
e.preventDefault();
$.ajax({
type: $(this).attr('method'),
dataType: 'html',
cache: false,
url: "../Scripts/emailtester.php",
data: {email:email},
success: function(data){
testFirstResults(data);
}
});
});
$("#submissionform").submit(function(e){
var origEmail = $("#go").val();
var confirmEmail = $("#confirmemail").val();
var name = $("#name").val();
var age = $("#age").val();
var gender = $("#gender").val();
var country = $("#country").val();
var catcher = $("#aicatcher").val();
var addition = $("#addition").val();
endTime = jQuery.now();
submissionTime = endTime - startTime;
e.preventDefault();
$.ajax({
type: "POST",
dataType: 'json',
cache: false,
url: "../Scripts/confirmform.php",
data: {
origEmail: origEmail,
confirmEmail: confirmEmail,
name: name,
age: age,
gender: gender,
country: country,
catcher: catcher,
addition: addition,
mathAnswer: mathAnswer,
submissionTime: submissionTime
},
success: function(data) {
testSecondResults(data);
},
});
});
$("#cancel").click(function(){
$("#blackoverlay").fadeOut(300);
$("#errormessage").empty();
$("#submissionform").fadeOut(300);
});
});