diff --git a/components/register.html b/components/register.html index 4a614a5..a864eea 100644 --- a/components/register.html +++ b/components/register.html @@ -3,10 +3,9 @@
diff --git a/db/datahandler.php b/db/datahandler.php index 49f6862..cf7430e 100644 --- a/db/datahandler.php +++ b/db/datahandler.php @@ -32,7 +32,7 @@ class DataHandler $stmtCities->bind_param("sss", null, $postalcode, $coty); if ($stmtUser->execute() && $stmtCities->execute() && $stmtAddress->execute()) { - return true; + return $data; } else { return false; } diff --git a/js/registration.js b/js/registration.js index 7e8ff42..7397df5 100644 --- a/js/registration.js +++ b/js/registration.js @@ -1,7 +1,7 @@ const password = document.getElementById('password'); const password2 = document.getElementById('password2'); - password2.addEventListener('change', checkPassword); + const form = document.getElementById('marmeladenLadenRegisterForm'); form.addEventListener('submit', register); @@ -15,16 +15,21 @@ async function register(event) { //create object with form data const data = {}; formData.forEach((value, key) => data[key] = value); - + //log data on console + datastring = JSON.stringify(data); + console.log(data); + console.log(datastring); //send data to php with Ajax $.ajax({ - url: '../logic/serviceLogic.php', + url: '../logic/registerLogic.php', type: 'POST', - data: {method: 'register', data: data}, - datatype: 'json', + data: datastring, + cache: false, + datatype: 'text', success: function (response) { - if (response === 'success') { - window.location.replace('index.html'); + console.log(response); + if (response == 'success') { + window.location.href = '../index.html'; } else { alert(response); } @@ -33,8 +38,6 @@ async function register(event) { } } - - async function checkPassword() { if (password.value != password2.value) { password2.setCustomValidity('Passwords do not match'); diff --git a/logic/registerLogic.php b/logic/registerLogic.php new file mode 100644 index 0000000..f3314a8 --- /dev/null +++ b/logic/registerLogic.php @@ -0,0 +1,46 @@ +email); + $password = password_hash(testinput($data->password), PASSWORD_DEFAULT); + $email = testinput($data->email); + $phone = testinput($data->phone); + $salutation = testinput($data->salutation); + $firstname = testinput($data->firstname); + $lastname = testinput($data->lastname); + $street = testinput($data->street); + $postalcode = testinput($data->postalcode); + $city = testinput($data->city); + $role = "customer"; + + registerUser($username, $password, $email, $phone, $salutation, $firstname, $lastname, $street, $postalcode, $city, $role); + + function registerUser($username, $password, $email, $phone, $salutation, $firstname, $lastname, $street, $postalcode, $city, $role) + { + + require($_SERVER['DOCUMENT_ROOT'] . '/config/setupDBAccess.php'); + + $sql2 = "INSERT IGNORE INTO `cities` (`postalcode`, `name`) VALUES (?,?)"; + $sql = "INSERT INTO `user` (`username`, `password`, `email`, `phone`, `salutation`, `firstname`, `lastname`, `address`, `plz`, `role`) VALUES (?,?,?,?,?,?,?,?,?,?)"; + + + $stmtUser = $db->prepare($sql); + $stmtCities = $db->prepare($sql2); + + $stmtUser->bind_param("ssssssssss", $username, $password, $email, $phone, $salutation, $firstname, $lastname, $street, $postalcode, $role); + $stmtCities->bind_param("is", $postalcode, $city); + + if ($stmtCities->execute() && $stmtUser->execute()) { + $response = "success"; + } else { + $response = "failure"; + } + $stmtUser->close(); + $stmtCities->close(); + $db->close(); + + echo $response; + + } \ No newline at end of file