<?php
session_start();
if (isset($_POST['email']) && isset($_POST['password']) && isset($_POST['confirmpassword']) && isset($_POST['firstname']) && isset($_POST['lastname']) && isset($_POST['gender']) && isset($_POST['preferred_course']) && isset($_POST['country']) && isset($_POST['working_experiences'])) {
$email = $password = $confirmPassword = $firstname = $lastname = $gender = $preferredCourse = $country = $workingExperiences = ""; // Define variables with empty values
// if a fields is validated, will be assigned to 1
$email_validated = $password_validated = $confirm_password_validated = $firstname_validated = $lastname_validated = $gender_validated = $course_validated = $country_validated = $validated = 0;
$we_title_validated = $we_start_date_validated = $we_end_date_validated = $we_emp_validated = $we_desc_validated = 0;
$results = []; // form validation results
// Get inputs from the register form
$email = validateData($_POST['email']);
$password = $_POST['password'];
$confirmPassword = $_POST['confirmpassword'];
$firstname = validateData($_POST['firstname']);
$lastname = validateData($_POST['lastname']);
$gender = $_POST['gender'];
$preferredCourse = $_POST['preferred_course'];
$country = validateData($_POST['country']);
$workingExperiences = $_POST['working_experiences'];
// Validate email address
if (empty($email)) {
$validated = 0;
$results += array('emailerror' => "Please enter a valid email address");
} elseif (validateEmail($email) == false) {
$validated = 0;
$results += array('emailerror' => "Invalid email format. eg: alex@gmail.com");
} else {
$validated = 1;
}
// Validate password and confirm it
if (empty($password)) {
$validated = 0;
$results += array('passworderror' => "Password should be at lease 8 characters with one capital letter, a number and a special character");
} elseif (!validatePassword($password)) {
$validated = 0;
$results += array('passworderror' => "Password should be at lease 8 characters with one capital letter, a number and a special character");
} elseif (empty($confirmPassword)) {
$validated = 0;
$results += array('confirmpassworderror' => "Please confirm your password");
} elseif ($password != $confirmPassword) {
$validated = 0;
$results += array('confirmpassworderror' => "Passwords does not match. Please check again");
} else {
$validated = 1;
}
// Validate first name
if (empty($firstname)) {
$validated = 0;
$results += array('firstnameerror' => "Please enter your first name");
} else {
$validated = 1;
}
// Validate last name
if (empty($lastname)) {
$validated = 0;
$results += array('lastnameerror' => "Please enter your last name");
} else {
$validated = 1;
}
// Validate gender
if (empty($gender)) {
$validated = 0;
$results += array('gendererror' => "Please select a gender");
} else {
$validated = 1;
}
// Validate preferred course
if (empty($preferredCourse)) {
$validated = 0;
$results += array('preferredcourseerror' => "Please select at least one preferred course");
} else {
$validated = 1;
}
// Validate country
if ($country == "") {
$validated = 0;
$results += array('countryerror' => "Please select your country");
} else {
$validated = 1;
}
// Validate working experiences
for ($i = 0; $i < sizeof($workingExperiences); $i++) {
if (empty($workingExperiences[$i]['title'])) {
$we_title_validated = 0;
$results += array('wetitleerror' => "Please enter the work experience title/profession");
} else {
$we_title_validated = 1;
}
if (empty($workingExperiences[$i]['start_date'])) {
$we_start_date_validated = 0;
$results += array('wesderror' => "Please enter the start date of your work experience");
} elseif (!validateDate($workingExperiences[$i]['start_date'])) {
$we_start_date_validated = 0;
$results += array('wesderror' => "Invalid date format. Please use the format of dd/mm/yyyy");
} elseif (checkFutureDate($workingExperiences[$i]['start_date']) == true) {
$we_start_date_validated = 0;
$results += array('wesderror' => "Please select a date in the past");
} else {
$we_start_date_validated = 1;
}
if (empty($workingExperiences[$i]['end_date'])) {
$we_end_date_validated = 0;
$results += array('weederror' => "Please enter the end date of your work experience");
} elseif (!validateDate($workingExperiences[$i]['end_date'])) {
$we_end_date_validated = 0;
$results += array('weederror' => "Invalid date format. Please use the format of dd/mm/yyyy");
} elseif (checkFutureDate($workingExperiences[$i]['end_date']) == true) {
$we_end_date_validated = 0;
$results += array('weederror' => "Please select a date in the past");
} elseif (!isStartLessThanEndDate($workingExperiences[$i]['start_date'], $workingExperiences[$i]['end_date'])) {
$we_end_date_validated = 0;
$results += array('weederror' => "End date cannot be before start date");
} else {
$validated = 1;
}
if (empty($workingExperiences[$i]['employer'])) {
$we_emp_validated = 0;
$results += array('weemperror' => "Please state your employer");
} else {
$we_emp_validated = 1;
}
if (empty($workingExperiences[$i]['description'])) {
$we_desc_validated = 0;
$results += array('wedescerror' => "Please add a little bit about your experience");
} else {
$we_desc_validated = 1;
}
}
// sleep(10);
if (
$we_title_validated === 1 &&
$we_start_date_validated === 1 &&
$we_end_date_validated === 1 &&
$we_emp_validated === 1 &&
$we_desc_validated === 1
) {
// Save the form data to a session as an array
$form_data = array(
"Email" => $email,
"Password" => $password,
"First Name" => $firstname,
"Last Name" => $lastname,
"Gender" => $gender,
"Preferred Courses" => $preferredCourse,
"Country" => $country
);
$_SESSION['register_form_data'] = $form_data;
$results += array('success' => 1);
echo json_encode($results);
} else {
echo json_encode($results);
}
// echo json_encode($results);
}
function validateData($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function validateEmail($email)
{
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
function validatePassword($password)
{
// Password should be at lease 8 characters with one capital letter, a number and a special character",
$uppercase = preg_match('@[A-Z]@', $password);
$lowercase = preg_match('@[a-z]@', $password);
$number = preg_match('@[0-9]@', $password);
$specialChars = preg_match('@[^\w]@', $password);
if (!$uppercase || !$lowercase || !$number || !$specialChars || strlen($password) < 8) {
return false;
} else {
return true;
}
}
function validateDate($date)
{
$matches = array();
$pattern = '/^([0-9]{1,2})\\/([0-9]{1,2})\\/([0-9]{4})$/';
if (!preg_match($pattern, $date, $matches)) return false;
if (!checkdate($matches[2], $matches[1], $matches[3])) return false;
return true;
}
function checkFutureDate($date)
{
// Verify the selected date is not a future date
$inputdate = date_format(date_create_from_format('d/m/Y', $date), 'Y/m/d');
$currentdate = date('Y/m/d', strtotime("now"));
if ($inputdate < $currentdate) {
return false;
} else {
return true;
}
}
function isStartLessThanEndDate($sdate, $edate)
{
// Check whether the start date is less than end date
$sdate = date_format(date_create_from_format('d/m/Y', $sdate), 'Y/m/d');
$edate = date_format(date_create_from_format('d/m/Y', $edate), 'Y/m/d');
if ($sdate < $edate) {
return true;
} else {
return false;
}
}