JezK
Edit File: categoriesManagement.php
<?php include("header.php"); $categories = $videocon->categories(); ?> <style> div.dataTables_wrapper div.dataTables_filter { position: absolute; top: 132px; } </style> <section class="section-padding"> <div class="container mt-0 mb-5 mx-3 pt-2 bg-white"> <div class="row mt-5 pt-4"> <div class="col-xl-6 pt-2"> <a> </a> <a> </a> <a> </a> </div> <div class="col-xl-6 d-flex justify-content-end"> <a href="editCategory.php" class="btn btn-primary">Add Category</a> </div> </div> <div class="table-responsive"> <table id="dataTable" class="table table-striped table-borderless " style="width:100%"> <thead class="tableBackground"> <tr> <th>S.No</th> <th>CATEGORY IMAGE</th> <th>CATEGORY NAME</th> <th>ADDED DATE</th> <th>ACTION</th> </tr> </thead> <tbody> <?php $j = 0; foreach ($categories as $k => $v) { $j++; $catimg = ''; if ($v['image'] != '' && file_exists('uploads/' . $v['image'])) { $catimg = 'uploads/' . $v['image']; } else { $catimg = "img/dummybusinesspic.png"; } ?> <tr> <td><?php echo $j; ?></td> <td><img src="<?php echo $catimg; ?>" class="img" height="66px" /></td> <td><?php echo $v['name']; ?></td> <td><?php echo date('m-d-Y', strtotime($v['date'])); ?></td> <td> <?php if ($v['delete_status'] == 1) { echo '<span>deleted</span> '; } else { ?> <span> <a href="categoryDetails.php?cid=<?php echo $videocon->encrypt_decrypt('encrypt', $v['id']); ?>" class="viewStyles">View</a> </span> <span> <a href="editCategory.php?cid=<?php echo $videocon->encrypt_decrypt('encrypt', $v['id']); ?>" class="viewStyles">Edit</a> </span> <span> <a href="#" class="viewStyles" onclick="deleteCategory(this, '<?php echo $v['id']; ?>')">delete</a> </span> <?php } ?> </td> </tr> <?php } ?> </tbody> </table> </div> </div> </section> <?php include("footer.php"); ?> <script type="text/javascript"> function deleteCategory(event, categoryId) { // event.preventDefault(); // Prevent default link action console.log("Category ID:", categoryId); if (confirm("Are you sure you want to delete this category?")) { // Check if the category has associated businesses before deletion fetch(`ajax.php?action=categoryCheck&catID=${categoryId}`) .then(response => response.json()) // Expecting JSON response .then(data => { console.log("Category Check Data:", data); if (data.exists) { alert("This category holds businesses and cannot be deleted."); } else { // Proceed with deletion fetch(`ajax.php?action=deleteCategory&catID=${categoryId}`) .then(response => response.json()) // Expect JSON response .then(deleteData => { console.log("Delete Response:", deleteData); if (deleteData.success) { alert("Category deleted successfully!"); location.reload(); // element.closest("tr").remove(); // Remove the row from table } else { alert("Failed to delete category."); } }) .catch(error => console.error("Error deleting category:", error)); } }) .catch(error => console.error("Error checking category:", error)); } } </script>