From 4e6af105f54897b07310bd18c85ea8d28ca5754c Mon Sep 17 00:00:00 2001 From: StockiP Date: Mon, 2 May 2022 09:21:19 +0200 Subject: [PATCH] added logic / Form stuff --- components/register.html | 12 ++++++-- db/datahandler.php | 27 ++++++++--------- js/app.js | 19 ++++++++++++ js/app.ts | 3 -- js/registration.js | 63 +++++++++++++++++----------------------- logic/serviceLogic.php | 46 +++++++++++++++++++++++++++++ 6 files changed, 115 insertions(+), 55 deletions(-) create mode 100644 js/app.js create mode 100644 logic/serviceLogic.php diff --git a/components/register.html b/components/register.html index 1b65ac4..014be3b 100644 --- a/components/register.html +++ b/components/register.html @@ -23,7 +23,7 @@
- +
@@ -31,7 +31,12 @@
- +
@@ -59,4 +64,5 @@
-
\ No newline at end of file +
+ \ No newline at end of file diff --git a/db/datahandler.php b/db/datahandler.php index ffd6fac..a6f9d5c 100644 --- a/db/datahandler.php +++ b/db/datahandler.php @@ -4,20 +4,21 @@ include($_SERVER['DOCUMENT_ROOT'] . '/logic/testinput.php'); class DataHandler { - public function registerUser($username, $password, $email, $phone, $salutation, $firstname, $lastname, $role, $street, $streetnumber, $postalcode, $country) + public function registerUser($data) { - $username = testinput($username); - $password = testinput($password); - $email = testinput($email); - $phone = testinput($phone); - $salutation = testinput($salutation); - $firstname = testinput($firstname); - $lastname = testinput($lastname); - $role = testinput($role); - $street = testinput($street); - $streetnumber = testinput($streetnumber); - $postalcode = testinput($postalcode); - $country = testinput($country); + $username = testinput($data->username); + $password = testinput($data->password); + $email = testinput($data->email); + $phone = testinput($data->phone); + $salutation = testinput($data->salutation); + $firstname = testinput($data->firstname); + $lastname = testinput($data->lastname); + $street = testinput($data->street); + $streetnumber = testinput($data->streetnumber); + $postalcode = testinput($data->postalcode); + $country = testinput($data->country); + + $password = password_hash($password, PASSWORD_DEFAULT); require($_SERVER['DOCUMENT_ROOT'] . '/config/setupDBAccess.php'); diff --git a/js/app.js b/js/app.js new file mode 100644 index 0000000..21d383b --- /dev/null +++ b/js/app.js @@ -0,0 +1,19 @@ +"use strict"; +$(document).ready(function () { + $('#mmlMainContent').load('../components/homepage.html'); + $('#top_nav_bar').load('../components/top_nav.html'); + $('#main_nav_bar').load('../components/main_nav.html', function () { + var _a, _b, _c; + (_a = document.getElementById('registerLink')) === null || _a === void 0 ? void 0 : _a.addEventListener('click', function () { + $('#mmlMainContent').load('../components/register.html'); + }); + (_b = document.getElementById('homeLink')) === null || _b === void 0 ? void 0 : _b.addEventListener('click', function () { + $('#mmlMainContent').load('../components/homepage.html'); + }); + (_c = document.getElementById('loginLink')) === null || _c === void 0 ? void 0 : _c.addEventListener('click', function () { + $('#mmlMainContent').load('../components/login.html'); + }); + }); + $('#marmeladenladen_footer').load('../components/footer.html'); + $('#marmeladenladen_search').load('../components/modal.html'); +}); diff --git a/js/app.ts b/js/app.ts index 524fb89..a1b9765 100644 --- a/js/app.ts +++ b/js/app.ts @@ -1,5 +1,3 @@ - - $(document).ready(function() { $('#mmlMainContent').load('../components/homepage.html'); $('#top_nav_bar').load('../components/top_nav.html'); @@ -14,7 +12,6 @@ $(document).ready(function() { $('#mmlMainContent').load('../components/login.html'); },); }); - $('#mmlMainContent').load('../components/ftProducts.html'); $('#marmeladenladen_footer').load('../components/footer.html'); $('#marmeladenladen_search').load('../components/modal.html'); }); diff --git a/js/registration.js b/js/registration.js index 198ef06..c2ae4fd 100644 --- a/js/registration.js +++ b/js/registration.js @@ -1,41 +1,42 @@ -const email = document.getElementById('email'); const password = document.getElementById('password'); const password2 = document.getElementById('password2'); + + password2.addEventListener('change', checkPassword); const form = document.getElementById('marmeladenLadenRegisterForm'); form.addEventListener('submit', register); +async function register(event) { + event.preventDefault(); -function register() { + //check password with checkPassword function + if (checkPassword()) { + //get form data + const formData = new FormData(form); + //create object with form data + const data = {}; + formData.forEach((value, key) => data[key] = value); - - $.ajax({ - url: 'php/registration.php', - type: 'POST', - data: { - email: email.value, - password: password.value, - password2: password2.value - }, - success: function (data) { - if (data == 'success') { - window.location.href = 'index.html'; - } else { - alert(data); + //send data to php with Ajax + $.ajax({ + url: '../logic/serviceLogic.php', + type: 'POST', + data: {method: 'register', data: data}, + datatype: 'json', + success: function (response) { + if (response === 'success') { + window.location.replace('index.html'); + } else { + alert(response); + } } - } - }); - return false; + }); + } } - var formData = new FormData(form); - const jSONdata = JSON.stringify(Object.fromEntries(formData)); + - //print elements of jSONdata - console.log(jSONdata); - - -function checkPassword() { +async function checkPassword() { if (password.value != password2.value) { password2.setCustomValidity('Passwords do not match'); return false; @@ -43,14 +44,4 @@ function checkPassword() { password2.setCustomValidity(''); return true; } -} - -function checkemail() { - if (email.value != '') { - email.setCustomValidity(''); - return true; - } else { - email.setCustomValidity('Email is required'); - return false; - } } \ No newline at end of file diff --git a/logic/serviceLogic.php b/logic/serviceLogic.php new file mode 100644 index 0000000..0e43d3c --- /dev/null +++ b/logic/serviceLogic.php @@ -0,0 +1,46 @@ +handleUserRequests($method, $param); +if ($result == null) { + response("GET", 400, null); +} else { + response("GET", 200, $result); +} + +function response($method, $status, $data) +{ + + header('Content-Type: application/json'); + switch ($method) { + case 'GET': + http_response_code($status); + echo json_encode($data); + break; + case 'POST': + http_response_code($status); + echo json_encode($data); + break; + case 'PUT': + http_response_code($status); + echo json_encode($data); + break; + case 'DELETE': + http_response_code($status); + echo json_encode($data); + break; + default: + http_response_code(400); + echo json_encode(array("message" => "Invalid method")); + break; + } +} + +?> \ No newline at end of file