From 236d8954981879a6b0499415821feb9726d1decf Mon Sep 17 00:00:00 2001 From: johnnyq Date: Sat, 16 Aug 2025 01:02:16 -0400 Subject: [PATCH] Reworked the ajax-modal function to be more simplified and use less data attributes --- includes/modal_footer.php | 1 + js/ajax_modal.js | 57 +++++++++++++++++++++++++++++++++++++++ user/clients.php | 25 +++++++---------- 3 files changed, 68 insertions(+), 15 deletions(-) diff --git a/includes/modal_footer.php b/includes/modal_footer.php index dd8e513b..b3fc7301 100644 --- a/includes/modal_footer.php +++ b/includes/modal_footer.php @@ -6,4 +6,5 @@ // Return the title and content as a JSON response echo json_encode(['content' => $content]); + exit(); ?> \ No newline at end of file diff --git a/js/ajax_modal.js b/js/ajax_modal.js index 357a5e96..5a70dd93 100644 --- a/js/ajax_modal.js +++ b/js/ajax_modal.js @@ -12,6 +12,63 @@ */ +// New Way +$(document).on('click', '.ajax-modal', function (e) { + e.preventDefault(); + + const $trigger = $(this); + const modalUrl = $trigger.data('modal-url'); + const modalSize = $trigger.data('modal-size') || 'md'; + const modalId = 'ajaxModal_' + new Date().getTime(); + + // Show loading spinner while fetching content + const loadingSpinner = ` + `; + $('.content-wrapper').append(loadingSpinner); + + // Make AJAX request + $.ajax({ + url: modalUrl, + method: 'GET', + dataType: 'json', + success: function (response) { + $('#modal-loading-spinner').remove(); + + if (response.error) { + alert(response.error); + return; + } + + // Build modal wrapper + const modalHtml = ` + `; + + $('.content-wrapper').append(modalHtml); + const $modal = $('#' + modalId); + $modal.modal('show'); + + // Remove modal after it's closed + $modal.on('hidden.bs.modal', function () { + $(this).remove(); + }); + }, + error: function (xhr, status, error) { + $('#modal-loading-spinner').remove(); + alert('Error loading modal content. Please try again.'); + console.error('Modal AJAX Error:', status, error); + } + }); +}); + +// OLD Way $(document).on('click', '[data-toggle="ajax-modal"]', function (e) { e.preventDefault(); diff --git a/user/clients.php b/user/clients.php index 90f2bb6d..a65fb66e 100644 --- a/user/clients.php +++ b/user/clients.php @@ -104,7 +104,7 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); -
+
@@ -289,8 +289,8 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
- - text-nowrap bg-light"> +
+ bg-light"> - - + - @@ -591,10 +589,7 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
@@ -514,8 +514,8 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
+ +