[ Root System Explorer ]
Location:
Root
/
home
/
u456045770
/
domains
/
momoeshop.com
/
public_html
/
ads
+ Folder
+ File
Upload
Editing: listing.php
<?php error_reporting(E_ALL); ini_set('display_errors', 1); ini_set('display_startup_errors', 1); // If editing, fetch existing listing data $listing = []; if(isset($_GET['id'])){ $id = $_GET['id']; $qry = $conn->query("SELECT * FROM listings WHERE id = '$id'"); if($qry->num_rows > 0){ $listing = $qry->fetch_assoc(); } } // Fetch sectors grouped by city $sectors_by_city = []; $q = $conn->query("SELECT * FROM `sectors` WHERE status = 1 ORDER BY city, sector"); while($r = $q->fetch_assoc()){ $city = $r['city']; if(!isset($sectors_by_city[$city])) $sectors_by_city[$city] = []; $sectors_by_city[$city][] = $r; } // Define categories for PRODUCTS (physical items) $product_categories = [ 'Vehicles' => [ 'Cars', 'Motorcycles', 'Bicycles', 'Commercial Vehicles', 'Spare Parts' ], 'Electronics' => [ 'Mobile Phones', 'Tablets', 'Laptops', 'TVs', 'Cameras', 'Audio & Headphones' ], 'Home & Garden' => [ 'Decor', 'Kitchen Appliances', 'Gardening' ], 'Fashion' => [ "Men's Clothing", "Women's Clothing", "Kids' Clothing", 'Shoes' ], 'Health & Beauty' => [ 'Makeup', 'Skincare', 'Haircare', 'Fitness Equipment' ], 'Sports & Hobbies' => [ 'Sports Equipment', 'Musical Instruments', 'Toys' ], 'Others' => [ 'Other Items' ] ]; // Define categories for SERVICES $service_categories = [ 'Services' => [ 'Repair Services', 'Cleaning', 'Moving & Storage', 'Lessons & Tutoring', 'Event Services', 'Pet Services' ] ]; ?> <section class="py-5"> <div class="container"> <div class="card rounded-0"> <div class="card-body"> <div class="w-100 justify-content-between d-flex"> <h4><b><?php echo isset($listing['id']) ? 'Edit' : 'Add New'; ?> Listing</b></h4> <a href="./?p=listings" class="btn btn-dark btn-flat"><i class="fa fa-angle-left"></i> Back to List</a> </div> <hr class="border-warning"> <form action="" id="listing_form" enctype="multipart/form-data"> <input type="hidden" name="id" value="<?php echo $listing['id'] ?? '' ?>"> <div class="row"> <div class="col-md-6"> <div class="form-group"> <label for="title" class="control-label">Title *</label> <input type="text" name="title" class="form-control" value="<?php echo htmlspecialchars($listing['title'] ?? '') ?>" required> </div> <div class="form-group"> <label for="description" class="control-label">Description</label> <textarea name="description" class="form-control" rows="3"><?php echo htmlspecialchars($listing['description'] ?? '') ?></textarea> </div> <div class="form-group"> <label for="price" class="control-label">Price ($) *</label> <input type="number" step="0.01" name="price" class="form-control" value="<?php echo $listing['price'] ?? '' ?>" required> </div> <!-- City Dropdown (NEW) --> <div class="form-group"> <label for="city" class="control-label">City *</label> <select name="city" id="city" class="custom-select" required> <option value="">-- Select City --</option> <?php foreach(array_keys($sectors_by_city) as $city_name): ?> <option value="<?php echo htmlspecialchars($city_name) ?>" <?php echo (isset($listing['city']) && $listing['city'] == $city_name) ? 'selected' : '' ?>> <?php echo htmlspecialchars($city_name) ?> </option> <?php endforeach; ?> </select> </div> <!-- Sector Dropdown (NEW - dynamically populated) --> <div class="form-group"> <label for="sector_id" class="control-label">Sector</label> <select name="sector_id" id="sector_id" class="custom-select"> <option value="">-- Select City First --</option> </select> </div> <div class="form-group"> <label for="listing_type" class="control-label">Type *</label> <select name="listing_type" class="custom-select" required> <option value="product" <?php echo (isset($listing['listing_type']) && $listing['listing_type'] == 'product') ? 'selected' : '' ?>>Product (Physical Item)</option> <option value="service" <?php echo (isset($listing['listing_type']) && $listing['listing_type'] == 'service') ? 'selected' : '' ?>>Service</option> </select> </div> <div class="form-group"> <label for="category" class="control-label">Category *</label> <select name="category" class="custom-select" required> <!-- Options will be populated by JavaScript --> </select> </div> <div class="form-group" id="condition-group"> <label for="condition" class="control-label">Condition (for products)</label> <select name="condition" class="custom-select"> <option value="">-- Select --</option> <option value="New" <?php echo (isset($listing['condition']) && $listing['condition'] == 'New') ? 'selected' : '' ?>>New</option> <option value="Like New" <?php echo (isset($listing['condition']) && $listing['condition'] == 'Like New') ? 'selected' : '' ?>>Like New</option> <option value="Used" <?php echo (isset($listing['condition']) && $listing['condition'] == 'Used') ? 'selected' : '' ?>>Used</option> <option value="For Parts" <?php echo (isset($listing['condition']) && $listing['condition'] == 'For Parts') ? 'selected' : '' ?>>For Parts / Not Working</option> </select> </div> </div> <div class="col-md-6"> <div class="form-group" id="stock-group"> <label for="stock_quantity" class="control-label">Stock Quantity (for products)</label> <input type="number" name="stock_quantity" class="form-control" value="<?php echo $listing['stock_quantity'] ?? 0 ?>" min="0"> </div> <div class="form-group"> <label for="listing_image" class="control-label">Image</label> <input type="file" name="listing_image" class="form-control-file" accept="image/*"> <?php if(!empty($listing['image_path'])): ?> <div class="mt-2"> <img src="<?php echo htmlspecialchars($listing['image_path']) ?>" alt="Current Image" style="max-height: 100px;"> </div> <?php endif; ?> </div> <div class="form-group form-check"> <input type="checkbox" class="form-check-input" name="status" id="status" value="1" <?php echo (!isset($listing['status']) || $listing['status'] == 1) ? 'checked' : '' ?>> <label class="form-check-label" for="status">Active</label> </div> </div> </div> <div class="form-group d-flex justify-content-end"> <button class="btn btn-dark btn-flat">Save Listing</button> </div> </form> </div> </div> </div> </section> <script> // Pass PHP arrays to JavaScript var productCategories = <?php echo json_encode($product_categories); ?>; var serviceCategories = <?php echo json_encode($service_categories); ?>; var sectorsByCity = <?php echo json_encode($sectors_by_city); ?>; var selectedCategory = <?php echo json_encode($listing['category'] ?? ''); ?>; var selectedType = <?php echo json_encode($listing['listing_type'] ?? 'product'); ?>; var selectedCity = <?php echo json_encode($listing['city'] ?? ''); ?>; var selectedSector = <?php echo json_encode($listing['sector_id'] ?? ''); ?>; $(function(){ // Initialize Select2 if used (optional) // $('.select2').select2({ placeholder: "Please Select here", width: "100%" }); // Function to populate category dropdown based on type function populateCategories(type) { var $categorySelect = $('select[name="category"]'); $categorySelect.empty(); $categorySelect.append('<option value="">-- Select Category --</option>'); var categories = (type === 'product') ? productCategories : serviceCategories; $.each(categories, function(mainCategory, subcategories) { var $optgroup = $('<optgroup>').attr('label', mainCategory); $.each(subcategories, function(index, sub) { var fullCat = mainCategory + ' - ' + sub; var $option = $('<option>').val(fullCat).text(sub); if (selectedCategory === fullCat && type === selectedType) { $option.prop('selected', true); } $optgroup.append($option); }); $categorySelect.append($optgroup); }); } // Function to update sector dropdown based on selected city function updateSectors() { var city = $('#city').val(); var $sectorSelect = $('#sector_id'); $sectorSelect.empty(); if (city && sectorsByCity[city]) { $sectorSelect.append('<option value="">-- Select Sector --</option>'); $.each(sectorsByCity[city], function(index, sector) { var selected = (String(sector.id) === String(selectedSector)) ? 'selected' : ''; $sectorSelect.append('<option value="' + sector.id + '" ' + selected + '>' + sector.sector + '</option>'); }); } else { $sectorSelect.append('<option value="">-- Select City First --</option>'); } } // Toggle condition/stock fields AND update category dropdown on type change function toggleFields() { var type = $('select[name="listing_type"]').val(); if(type === 'product') { $('#condition-group, #stock-group').show(); } else { $('#condition-group, #stock-group').hide(); $('select[name="condition"]').val(''); $('input[name="stock_quantity"]').val(0); } populateCategories(type); } // Initial population populateCategories(selectedType); $('select[name="listing_type"]').val(selectedType); toggleFields(); // Set city dropdown to saved value and trigger sector update if (selectedCity) { $('#city').val(selectedCity); } updateSectors(); // Populate sectors based on selected city (or show placeholder) // When city changes, update sectors $('#city').on('change', function() { updateSectors(); }); // When listing type changes $('select[name="listing_type"]').change(function() { toggleFields(); }); // Form submission $('#listing_form').submit(function(e){ e.preventDefault(); start_loader(); $('.err-msg').remove(); var formData = new FormData(this); // Optional: validate that sector is selected if city is chosen var city = $('#city').val(); var sectorId = $('#sector_id').val(); if (city && !sectorId) { var el = $('<div>').addClass("alert alert-danger err-msg").text("Please select a sector for the chosen city."); $('#listing_form').prepend(el); $('html, body').animate({scrollTop: 0}, 'fast'); end_loader(); return false; } $.ajax({ url: _base_url_ + "classes/Ads.php?f=save_listing", method: "POST", data: formData, dataType: "json", cache: false, contentType: false, processData: false, error: err => { console.log(err); alert_toast("An error occurred", 'error'); end_loader(); }, success: function(resp){ if(resp.status == 'success'){ alert_toast("Listing successfully saved", 'success'); setTimeout(function(){ location.href = './?p=listings'; }, 1000); } else if(resp.status == 'failed' && resp.msg){ var _err_el = $('<div>').addClass("alert alert-danger err-msg").text(resp.msg); $('#listing_form').prepend(_err_el); $('body, html').animate({scrollTop: 0}, 'fast'); end_loader(); } else { alert_toast("An error occurred", 'error'); end_loader(); } } }); }); }); </script>
SAVE CHANGES
[ CANCEL ]
Name
Type
Actions
.. (Parent Directory)
📄 job.php
FILE
Ren
[EDIT]
DEL
📄 listing.php
FILE
Ren
[EDIT]
DEL
📄 myads.php
FILE
Ren
[EDIT]
DEL
📄 property.php
FILE
Ren
[EDIT]
DEL