File manager - Edit - /var/www/order.cmtc.ac.th/user/index_2.php
Back
<?php session_start(); include('../config/db.php'); // ✅ ตรวจสอบสถานะระบบจอง $booking_row = $conn->query("SELECT setting_value FROM settings WHERE setting_key='booking_status'")->fetch_assoc(); $booking_status = $booking_row['setting_value'] ?? 'on'; // ✅ ตรวจสอบค่าสถานะ Free Order $setting = $conn->query("SELECT setting_value FROM settings WHERE setting_key='free_order'")->fetch_assoc(); $free_order_status = $setting['setting_value'] ?? 'off'; // ✅ ตรวจสอบว่ามีสินค้าในตะกร้า if(!isset($_SESSION['cart']) || count($_SESSION['cart']) == 0){ include('template_user_header.php'); echo "<div class='container py-5 text-center'> <h3>🛒 ยังไม่มีสินค้าในตะกร้า</h3> <a href='../index.php' class='btn btn-primary mt-3'>กลับไปเลือกสินค้า</a> </div>"; exit; } // ✅ ลบสินค้าออกจากตะกร้า if(isset($_GET['remove'])){ $rid = intval($_GET['remove']); unset($_SESSION['cart'][$rid]); header("Location: index.php"); exit; } // ✅ ดึงข้อมูลสินค้าที่อยู่ในตะกร้า $ids = implode(",", array_keys($_SESSION['cart'])); $res = $conn->query("SELECT * FROM products WHERE id IN ($ids)"); $cart_items = []; $total = 0; while($r = $res->fetch_assoc()){ $pid = $r['id']; $qty = $_SESSION['cart'][$pid]; $subtotal = $r['price'] * $qty; $total += $subtotal; $cart_items[] = [ 'id' => $pid, 'name' => $r['name'], 'price' => $r['price'], 'stock' => $r['stock'], 'img1' => $r['img1'], 'img2' => $r['img2'], 'qty' => $qty, 'subtotal' => $subtotal ]; } ?> <?php include('template_user_header.php'); ?> <!DOCTYPE html> <html lang="th"> <head> <meta charset="UTF-8"> <title>🛒 ตะกร้าสินค้า</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=Kanit:wght@300;400;600&display=swap" rel="stylesheet"> <style> body,*{font-family:'Kanit',sans-serif!important;} .table td, .table th { vertical-align: middle; } img.thumb {width:80px;border-radius:8px;margin:2px;} .product-images {display:flex;justify-content:center;align-items:center;gap:5px;} .img-label {font-size:13px;color:#555;text-align:center;} .section-divider { border-top: 3px dashed #ccc; margin: 40px 0; } .stock-label { font-size: 13px; color: #666; } .qty-group { display: flex; justify-content: center; align-items: center; gap: 5px; } .qty-btn { width: 32px; height: 32px; border: 1px solid #ccc; background-color: #f8f9fa; border-radius: 6px; cursor: pointer; font-weight: bold; transition: 0.2s; } .qty-btn:hover { background-color: #e0e0e0; } .qty-input { width: 60px !important; text-align: center; border: 1px solid #ccc; border-radius: 6px; } </style> </head> <body class="bg-light"> <div class="container py-5"> <h3 class="text-center mb-4">🪙 รายการสินค้าในตะกร้า</h3> <?php if($booking_status == 'off'): ?> <div class="alert alert-danger text-center fs-5 fw-bold"> ⛔ ขณะนี้ระบบปิดรับการจองเหรียญ </div> <?php endif; ?> <form action="checkout.php" method="post" id="cartForm"> <table class="table table-bordered bg-white align-middle"> <thead class="table-secondary text-center"> <tr> <th>รูป</th> <th>ชื่อเหรียญ</th> <th>ราคา (บาท)</th> <th>คงเหลือ</th> <th>จำนวน</th> <th>รวม</th> <th>ลบ</th> </tr> </thead> <tbody id="cartTable"> <?php foreach($cart_items as $item): ?> <tr data-id="<?=$item['id']?>"> <td class="text-center"> <div class="product-images"> <?php if($item['img1']): ?> <div> <img src="../uploads/products/<?=$item['img1']?>" class="thumb"><div class="img-label">ด้านหน้า</div> </div> <?php endif; ?> <?php if($item['img2']): ?> <div> <img src="../uploads/products/<?=$item['img2']?>" class="thumb"><div class="img-label">ด้านหลัง</div> </div> <?php endif; ?> </div> </td> <td><?=$item['name']?></td> <td class="text-end"><?=number_format($item['price'],2)?></td> <td class="text-center"><?=$item['stock']?> <span class="stock-label">ชิ้น</span></td> <td class="text-center"> <div class="qty-group"> <button type="button" class="qty-btn decrease">-</button> <input type="number" name="qty[<?=$item['id']?>]" value="<?=$item['qty']?>" min="1" max="<?=$item['stock']?>" class="form-control form-control-sm qty-input" readonly> <button type="button" class="qty-btn increase">+</button> </div> </td> <td class="text-end fw-bold subtotal"><?=number_format($item['subtotal'],2)?></td> <td class="text-center"><a href="?remove=<?=$item['id']?>" class="btn btn-sm btn-danger" onclick="return confirm('ลบสินค้าออกจากตะกร้า?')">ลบ</a></td> </tr> <?php endforeach; ?> <tr class="table-light"> <td colspan="5" class="text-end fw-bold">รวมทั้งหมด</td> <td id="cartTotal" class="text-end fw-bold text-success fs-5"><?=number_format($total,2)?></td> <td></td> </tr> </tbody> </table> <div class="d-flex justify-content-between mb-5"> <a href="../index.php" class="btn btn-secondary">⬅ เลือกสินค้าเพิ่ม</a> <button type="button" id="scrollToForm" class="btn btn-success <?=($booking_status=='off'?'disabled':'')?>" <?=($booking_status=='off'?'onclick="return false;"':'')?>> ดำเนินการต่อ ➜ </button> </div> <div class="section-divider"></div> <!-- ✅ ส่วนเลือกวิธีรับสินค้า --> <div id="checkoutSection"> <h4>📦 เลือกวิธีรับสินค้า</h4> <div class="mb-3"> <select name="receive_method" id="receiveMethod" class="form-control" required> <option value="">-- เลือกวิธีรับสินค้า --</option> <option value="รับสินค้าด้วยตนเอง">รับสินค้าด้วยตนเอง</option> <option value="จัดส่งทางไปรษณีย์">จัดส่งทางไปรษณีย์</option> </select> </div> <div id="customerInfo" class="border rounded p-3 bg-white" style="display:none;"> <h6 class="text-secondary mb-2">ข้อมูลผู้จอง / ผู้รับสินค้า</h6> <div class="row"> <div class="col-md-6 mb-3"> <label>ชื่อ-นามสกุล</label> <input type="text" name="fullname" id="fullname" class="form-control"> </div> <div class="col-md-6 mb-3"> <label>เบอร์โทรศัพท์มือถือ</label> <input type="text" name="phone" id="phone" class="form-control" maxlength="10" pattern="[0-9]{10}" placeholder="เช่น 0812345678"> </div> </div> <div class="mb-3"> <label>ที่อยู่จัดส่ง</label> <textarea name="address" id="address" class="form-control" rows="2"></textarea> </div> <div class="col-md-4 mb-3"> <label>รหัสไปรษณีย์</label> <input type="text" name="zipcode" id="zipcode" class="form-control" maxlength="5" pattern="[0-9]{5}" placeholder="เช่น 50200"> </div> </div> <div class="text-end mt-3"> <button type="submit" class="btn btn-primary px-4 py-2">💾 ยืนยันการจอง</button> </div> </div> </form> </div> <script> // ✅ อัปเดตจำนวนในตะกร้า (session) แบบ AJAX function updateCart(pid, qty){ fetch('update_cart.php', { method: 'POST', headers: {'Content-Type':'application/x-www-form-urlencoded'}, body: 'id='+pid+'&qty='+qty }); } // ✅ ปุ่มเพิ่ม/ลดจำนวนสินค้า + คำนวณอัตโนมัติ document.querySelectorAll('tr[data-id]').forEach(tr => { const pid = tr.getAttribute('data-id'); const input = tr.querySelector('.qty-input'); const price = parseFloat(tr.querySelector('.subtotal').textContent.replace(/,/g, '')) / parseInt(input.value); const max = parseInt(input.getAttribute('max')); const btnMinus = tr.querySelector('.decrease'); const btnPlus = tr.querySelector('.increase'); const updateTotals = () => { let total = 0; document.querySelectorAll('.subtotal').forEach(td => { total += parseFloat(td.textContent.replace(/,/g, '')) || 0; }); document.getElementById('cartTotal').textContent = total.toLocaleString(undefined, {minimumFractionDigits:2}); }; btnMinus.addEventListener('click', () => { let val = parseInt(input.value); if(val > 1){ val--; input.value = val; const subtotal = price * val; tr.querySelector('.subtotal').textContent = subtotal.toLocaleString(undefined, {minimumFractionDigits:2}); updateTotals(); updateCart(pid,val); } }); btnPlus.addEventListener('click', () => { let val = parseInt(input.value); if(val < max){ val++; input.value = val; const subtotal = price * val; tr.querySelector('.subtotal').textContent = subtotal.toLocaleString(undefined, {minimumFractionDigits:2}); updateTotals(); updateCart(pid,val); } else { alert('จำนวนที่เลือกเกินกว่าคงเหลือ (' + max + ' ชิ้น)'); } }); }); // ✅ เมื่อคลิก "ดำเนินการต่อ" document.getElementById('scrollToForm')?.addEventListener('click', () => { document.getElementById('checkoutSection').scrollIntoView({ behavior: 'smooth' }); }); // ✅ แสดงฟอร์มข้อมูลผู้รับเมื่อเลือกวิธีรับสินค้า document.getElementById('receiveMethod')?.addEventListener('change', function() { document.getElementById('customerInfo').style.display = (this.value !== '') ? 'block' : 'none'; }); // ✅ ตรวจสอบฟอร์มก่อนส่ง document.getElementById('cartForm').addEventListener('submit', function(e){ const method = document.getElementById('receiveMethod').value; const name = document.getElementById('fullname').value.trim(); const phone = document.getElementById('phone').value.trim(); const address = document.getElementById('address').value.trim(); const zipcode = document.getElementById('zipcode').value.trim(); const phoneRegex = /^[0-9]{10}$/; const zipRegex = /^[0-9]{5}$/; if(method === ''){ alert('กรุณาเลือกวิธีรับสินค้า'); e.preventDefault(); return false; } if(method === 'รับสินค้าด้วยตนเอง'){ if(name === '' || !phoneRegex.test(phone)){ alert('กรุณากรอกชื่อ และเบอร์โทรศัพท์มือถือให้ถูกต้อง (10 หลัก)'); e.preventDefault(); return false; } } if(method === 'จัดส่งทางไปรษณีย์'){ if(name === '' || !phoneRegex.test(phone) || address === '' || !zipRegex.test(zipcode)){ alert('กรุณากรอกข้อมูลให้ครบและถูกต้อง: ชื่อ, เบอร์โทร 10 หลัก, ที่อยู่, และรหัสไปรษณีย์ 5 หลัก'); e.preventDefault(); return false; } } }); </script> </body> </html>
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.42 |
proxy
|
phpinfo
|
Settings