diff --git a/src/api/index.php b/src/api/index.php index 0476c1a..d9905fe 100644 --- a/src/api/index.php +++ b/src/api/index.php @@ -49,10 +49,10 @@ $app->get("/timelineData/{timeline}", function (Request $request, Response $resp $json = json_encode($result); $response->getBody()->write($json); - //if it is an error give a 403 code since it can't find the data - if(array_key_exists("errorMessage", $result[0])) + //if it is an error give a 404 code since it can't find the data + if(array_key_exists("errorMessage", $result)) { - $response = $response->withStatus(403); + $response = $response->withStatus(404); } //use content type json to indicate json data on frontend. @@ -63,19 +63,191 @@ $app->get('/projectData', function (Request $request, Response $response) { global $projectData; - $result= $projectData->getProjectData(); + $result = $projectData->getProjectData(); $json = json_encode($result); $response->getBody()->write($json); - if(array_key_exists("errorMessage", $result[0])) + if(array_key_exists("errorMessage", $result)) { - $response = $response->withStatus(403); + $response = $response->withStatus(404); } //use content type json to indicate json data on frontend. return $response->withHeader("Content-Type", "application/json"); }); +$app->post('/contact', function (Request $request, Response $response) +{ + $data = $request->getParsedBody(); + if(empty($data["fName"]) || empty($data["lName"]) || empty($data["email"]) || empty($data["subject"]) || empty($data["message"])) + { + $response->getBody()->write(json_encode(array("errorMessage" => "Please fill out all the fields"))); + $response = $response->withStatus(400); + return $response->withHeader("Content-Type", "application/json"); + } + + if (!filter_var($data["email"], FILTER_VALIDATE_EMAIL)) + { + $response->getBody()->write(json_encode(array("errorMessage" => "Email is not the correct format"))); + $response = $response->withStatus(400); + return $response->withHeader("Content-Type", "application/json"); + } + + // email form filler/conatcter + $headers1 = "From: noreply@rohitpai.tech\r\n"; + $headers1 .= "Reply-To: rohit@rohitpai.tech\r\n"; + $headers1 .= "MIME-Version: 1.0\r\n"; + $headers1 .= "Content-Type: text/html; charset=UTF-8\r\n"; + + $message1 = " + + + {$data['subject']} + + + +

Thank you for filling out the form on my website, I will try to respond to your query as soon as I can.

+
+

Below is what you filled in for your record

+ + + + + + + + + + + + + + +
FirstnameLastnameEmailSubjectmessage
{$data['fName']}{$data['lName']}{$data['email']}{$data['subject']}
+
+
+

Regards,
Rohit Pai
rohit@rohitpai.tech + + + "; + + mail($data["email"], $data["subject"], $message1, $headers1); + + // email to me + $headers2 = "From: noreply@rohitpai.tech\r\n"; + $headers2 .= "Reply-To: {$data['email']}\r\n"; + $headers2 .= "MIME-Version: 1.0\r\n"; + $headers2 .= "Content-Type: text/html; charset=UTF-8\r\n"; + + $message2 = " + + + {$data['subject']} + + + +

{data['fName']} {data['lName']} filled in the form on the website, here is what they sent.

+ + + + + + + + + + + + + + +
FirstnameLastnameEmailSubjectmessage
{$data['fName']}{$data['lName']}{$data['email']}{$data['subject']}
+ + + "; + + mail("rohit@rohitpai.tech", "{$data['fName']} {$data['lName']} filled in the form", $message2, $headers2); + return $response->withStatus(201); +}); + $app->run(); diff --git a/src/api/timelineData.php b/src/api/timelineData.php index e3f2068..5686f13 100644 --- a/src/api/timelineData.php +++ b/src/api/timelineData.php @@ -25,7 +25,7 @@ class timelineData } else { - return array(array("errorMessage" => "Error, edu data not found")); + return array("errorMessage" => "Error, edu data not found"); } } @@ -44,7 +44,7 @@ class timelineData } else { - return array(array("errorMessage" => "Error, work data not found")); + return array("errorMessage" => "Error, work data not found"); } } diff --git a/src/js/main.js b/src/js/main.js index 889c91c..a5c6b45 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -212,4 +212,84 @@ document.addEventListener('DOMContentLoaded', () => // get projectData getProjectData(); +}); + +// contact form +document.querySelector("#contactForm").addEventListener("submit", e => +{ + e.preventDefault(); + let contactData = new FormData(); + contactData.append("fName", document.querySelector("#fName").value); + contactData.append("lName", document.querySelector("#lName").value); + contactData.append("email", document.querySelector("#email").value); + contactData.append("subject", document.querySelector("#subject").value); + contactData.append("message", document.querySelector("#message").value); + + if (document.querySelector("#fName").value.length == 0) + { + document.querySelector("#fName").classList.add("invalid"); + // please fill out all the fields + return; + } + else + { + document.querySelector("#fName").classList.remove("invalid"); + } + + if (document.querySelector("#lName").value.length == 0) + { + document.querySelector("#lName").classList.add("invalid"); + // please fill out all the fields + return; + } + else + { + document.querySelector("#lName").classList.remove("invalid"); + } + + if (document.querySelector("#email").value.length == 0) + { + document.querySelector("#email").classList.add("invalid"); + // please fill out all the fields + return; + } + else + { + document.querySelector("#email").classList.remove("invalid"); + } + + if (document.querySelector("#subject").value.length == 0) + { + document.querySelector("#subject").classList.add("invalid"); + // please fill out all the fields + return; + } + else + { + document.querySelector("#subject").classList.remove("invalid"); + } + + if (document.querySelector("#message").value.length == 0) + { + document.querySelector("#message").classList.add("invalid"); + // please fill out all the fields + return; + } + else + { + document.querySelector("#message").classList.remove("invalid"); + } + + fetch("/api/contact", + { + method: "POST", + body: contactData + }).then(res => + { + if(res.ok) + { + // show message box + } + }); + }); \ No newline at end of file