From 3ce72d14c6b6984db679aca39b2a1cc4929a6514 Mon Sep 17 00:00:00 2001 From: RiFav <13521075@std.stei.itb.ac.id> Date: Mon, 9 Oct 2023 03:29:42 +0700 Subject: [PATCH] fix: ajax config --- app/Views/event/update.php | 31 ++++++++++++++++++++++------- app/Views/profile/edit_profile.php | 32 ++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/app/Views/event/update.php b/app/Views/event/update.php index 95dd69c..60eaff8 100644 --- a/app/Views/event/update.php +++ b/app/Views/event/update.php @@ -50,18 +50,35 @@ const eventId = urlParams.get('id'); document.addEventListener("DOMContentLoaded", function(){ - // Fetch user data on page load - fetch('../../router.php?eventAction=getEvent&eventId=' + eventId) - .then(response => response.json()) - .then(data => { + var xhr = new XMLHttpRequest(); + + xhr.open('GET', '../../router.php?eventAction=getEvent&eventId=' + eventId, true); + + // Set up a function to run when the request is successfully completed + xhr.onload = function () { + if (xhr.status >= 200 && xhr.status < 300) { + // Parse the JSON + var data = JSON.parse(xhr.responseText); + + // Log and utilize data console.log(data); - // Populate the form fields with the data document.querySelector('input[name="eventName"]').value = data.event_name; document.querySelector('input[name="eventPrice"]').value = data.event_price; document.querySelector('input[name="eventDate"]').value = data.event_date; document.querySelector('input[name="eventLocation"]').value = data.event_location; - }) - .catch(error => console.error(error)); + } else { + // Handle error: log status text + console.log('Failed to fetch event:', xhr.statusText); + } + }; + + // Set up a function to handle any errors that may occur + xhr.onerror = function () { + console.error('Request failed:', xhr.statusText); + }; + + // Send the request + xhr.send(); }); const cancelButton = document.getElementById("cancelButton"); diff --git a/app/Views/profile/edit_profile.php b/app/Views/profile/edit_profile.php index 1756ea1..da5e7c5 100644 --- a/app/Views/profile/edit_profile.php +++ b/app/Views/profile/edit_profile.php @@ -31,17 +31,33 @@ <?php include '../template/footer.php'; ?> <script> document.addEventListener("DOMContentLoaded", function(){ - // Fetch user data on page load - fetch('../../router.php?userAction=getUser') - .then(response => response.json()) - .then(data => { - console.log(data); - // Populate the form fields with the data + var xhr = new XMLHttpRequest(); + + xhr.open('GET', '../../router.php?userAction=getUser', true); + + // Set up a function to run when the request is successfully completed + xhr.onload = function () { + if (xhr.status >= 200 && xhr.status < 300) { + // Parse the JSON + var data = JSON.parse(xhr.responseText); + + // Log and utilize data document.querySelector('input[name="user_name"]').value = data.user_name; document.querySelector('input[name="user_username"]').value = data.username; document.querySelector('input[name="user_email"]').value = data.user_email; - }) - .catch(error => console.error(error)); + } else { + // Handle error: log status text + console.log('Failed to fetch user:', xhr.statusText); + } + }; + + // Set up a function to handle any errors that may occur + xhr.onerror = function () { + console.error('Request failed:', xhr.statusText); + }; + + // Send the request + xhr.send(); }); const cancelButton = document.getElementById("cancelButton"); -- GitLab