File manager - Edit - /var/www/order.cmtc.ac.th/user/index_3.php
Back
<?php session_start(); include('../config/db.php'); // ✅ โหลดข้อมูลการจองก่อนหน้า (ถ้ามี) $checkout_old = $_SESSION['checkout'] ?? [ 'fullname' => '', 'phone' => '', 'address' => '', 'zipcode' => '', 'receive_method' => '' ]; // ✅ ตรวจสอบสถานะระบบจอง $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='pre_order_status'")->fetch_assoc(); $pre_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): ?> <?php $is_preorder = ($item['stock'] <= 0 && $pre_order_status == 'on'); $stock_text = $is_preorder ? "<span class='text-warning fw-bold'>Pre Order</span>" : ($item['stock'] > 0 ? number_format($item['stock'])." <span class='stock-label'>ชิ้น</span>" : "<span class='text-danger fw-bold'>สินค้าหมด</span>"); $max_stock = $is_preorder ? 99 : $item['stock']; // ถ้า Pre Order กำหนดจำนวนสูงสุดเป็น 99 ชิ้น ?> <tr data-id="<?=$item['id']?>" data-stock="<?=$item['stock']?>"> <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"><?=$stock_text?></td> <td class="text-center"> <div class="qty-group"> <button type="button" class="qty-btn decrease" <?=($item['stock'] == 0 && !$is_preorder)?'disabled':''?>>-</button> <input type="number" name="qty[<?=$item['id']?>]" value="<?=$item['qty']?>" min="1" max="<?=$max_stock?>" class="form-control form-control-sm qty-input" readonly> <button type="button" class="qty-btn increase" <?=($item['stock'] == 0 && !$is_preorder)?'disabled':''?>>+</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> </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="รับเหรียญด้วยตนเอง" <?=($checkout_old['receive_method']=='รับเหรียญด้วยตนเอง'?'selected':'')?>> รับเหรียญด้วยตนเอง </option> <option value="จัดส่งทางไปรษณีย์" <?=($checkout_old['receive_method']=='จัดส่งทางไปรษณีย์'?'selected':'')?>> จัดส่งทางไปรษณีย์ </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" value="<?=htmlspecialchars($checkout_old['fullname'])?>"> </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" value="<?=htmlspecialchars($checkout_old['phone'])?>"> </div> </div> <div class="mb-3"> <label>ที่อยู่จัดส่ง</label> <textarea name="address" id="address" class="form-control" rows="2"><?=htmlspecialchars($checkout_old['address'])?></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" value="<?=htmlspecialchars($checkout_old['zipcode'])?>"> </div> </div> <div class="text-end mt-3"> <button type="submit" class="btn btn-success px-4 py-2">ดำเนินการต่อ ➜</button> </div> </div> </form> </div> <script> // ✅ ฟังก์ชันอัปเดต session ผ่าน AJAX function updateServerCart(id, qty) { fetch('update_cart.php', { method: 'POST', headers: {'Content-Type': 'application/x-www-form-urlencoded'}, body: 'id=' + id + '&qty=' + qty }).then(res => res.json()) .then(data => console.log('Cart updated:', data)); } // ✅ ปุ่มเพิ่ม/ลดจำนวนเหรียญ + คำนวณอัตโนมัติ 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(); updateServerCart(pid, val); // ✅ อัปเดต session } }); 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(); updateServerCart(pid, val); // ✅ อัปเดต session } 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'; }); // ✅ ถ้ามีค่าเก่าจาก session ให้แสดงฟอร์มอัตโนมัติ document.addEventListener('DOMContentLoaded', function() { const oldMethod = "<?= $checkout_old['receive_method'] ?>"; if(oldMethod !== '') { document.getElementById('receiveMethod').value = oldMethod; document.getElementById('customerInfo').style.display = 'block'; } }); // ✅ ตรวจสอบฟอร์มก่อนส่ง 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.41 |
proxy
|
phpinfo
|
Settings