55ok

Mini

Direktori : /home/u307599615/domains/costumeonrent.in/public_html/
Upload File :
Current File : /home/u307599615/domains/costumeonrent.in/public_html/checkout.php

<?php
include("admin/connection.php");

// =======================
// FORM SUBMISSION HANDLER
// =======================
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // Sanitize input
  $name        = mysqli_real_escape_string($con, $_POST['name']);
  $email       = mysqli_real_escape_string($con, $_POST['email']);
  $phone       = mysqli_real_escape_string($con, $_POST['phone']);
  $address     = mysqli_real_escape_string($con, $_POST['address']);
  $payment     = mysqli_real_escape_string($con, $_POST['payment']);

  // Calculate subtotal
  $subtotal = 0;
  $cartQuery = mysqli_query($con, "SELECT * FROM cart ORDER BY id DESC");
  while ($item = mysqli_fetch_assoc($cartQuery)) {
    $subtotal += ($item['buy'] * $item['quantity']);
  }

  $discount   = 0; // (Add coupon logic here later if needed)
  $grandTotal = $subtotal - $discount;

  // Insert order into checkout_orders table
  $sql = "INSERT INTO checkout_orders 
          (name, email, phone, address, payment, subtotal, discount, grand_total) 
          VALUES 
          ('$name', '$email', '$phone', '$address', '$payment', '$subtotal', '$discount', '$grandTotal')";

  if (mysqli_query($con, $sql)) {
    header("Location: thankyou.php");
    exit;
  } else {
    die("Error: " . mysqli_error($con));
  }
}

// =======================
// INCLUDE HEADER
// =======================
include('include/header.php');

// Example user fetch (you can replace with session user id)
$id = 1;
$sql = "SELECT * FROM register WHERE id='$id'";
$query = mysqli_query($con, $sql);
$user = mysqli_fetch_assoc($query);
?>

<!-- ======================= -->
<!-- Checkout Page Layout -->
<!-- ======================= -->
<div class="checkout-container" style="display:flex; flex-wrap:wrap; gap:30px; padding:40px; background:#f8f8f8;">

  <!-- Left: Billing Form -->
  <div class="checkout-left" style="flex:1; min-width:320px; background:#fff; padding:25px; border-radius:10px;">
    <h2 style="margin-bottom:20px;">Billing Details</h2>

    <form action="" method="post">
      <label for="name">Full Name</label>
      <input type="text" id="name" name="name"
        value="<?= htmlspecialchars($user['Name'] ?? ''); ?>"
        placeholder="Enter your full name" required
        style="width:100%; margin-bottom:15px; padding:8px;">

      <label for="email">Email</label>
      <input type="email" id="email" name="email"
        value="<?= htmlspecialchars($user['Email'] ?? ''); ?>"
        placeholder="Enter your email" required
        style="width:100%; margin-bottom:15px; padding:8px;">

      <label for="phone">Phone Number</label>
      <input type="text" id="phone" name="phone"
        value="<?= htmlspecialchars($user['Phone'] ?? ''); ?>"
        placeholder="Enter your phone number" required
        style="width:100%; margin-bottom:15px; padding:8px;">

      <label for="address">Address</label>
      <textarea id="address" name="address"
        placeholder="Enter your address" required
        style="width:100%; margin-bottom:15px; padding:8px;"></textarea>

      <label for="payment">Payment Method</label>
      <select id="payment" name="payment" required
        style="width:100%; margin-bottom:20px; padding:8px;">
        <option value="credit_card">Credit Card</option>
        <option value="debit_card">Debit Card</option>
        <option value="upi">UPI</option>
        <option value="cod">Cash on Delivery</option>
      </select>

      <button type="submit" class="checkout-btn"
        style="width:100%; background:#28a745; color:#fff; border:none; padding:12px; border-radius:5px; font-size:16px; cursor:pointer;">
        Place Order
      </button>
    </form>
  </div>

  <!-- Right: Order Summary -->
  <div class="checkout-right" style="flex:1; min-width:320px; background:#fff; padding:25px; border-radius:10px;">
    <h2 style="margin-bottom:20px;">Order Summary</h2>

    <?php
    $subtotal = 0;
    $cartQuery = mysqli_query($con, "SELECT * FROM cart ORDER BY id DESC");

    if (mysqli_num_rows($cartQuery) > 0) {
      while ($item = mysqli_fetch_assoc($cartQuery)) {
        $name = htmlspecialchars($item['name']);
        $buy = (float)$item['buy'];
        $qty = (int)$item['quantity'];
        $total = $buy * $qty;
        $subtotal += $total;

        $productId = intval($item['product_id']);
        $productQuery = mysqli_query($con, "SELECT * FROM cart WHERE product_id = '$productId'");
        $productRow = mysqli_fetch_assoc($productQuery);

        // $images = !empty($productRow['Image']) ? json_decode($productRow['Image'], true) : [];
        // $firstImage = !empty($images[0]) ? $images[0] : 'default.jpg';
    ?>
        <div class="order-item" style="display:flex; align-items:center; gap:15px; margin-bottom:15px;">
          <!-- <img src="admin/assets/images/categorie/<?= htmlspecialchars($firstImage); ?>"
            alt="<?= $name; ?>" width="80" style="border-radius:5px;"> -->
          <div class="order-details">
            <p><strong><?= $name; ?></strong></p>
            <p><?= $qty; ?> × ₹<?= number_format($buy, 2); ?></p>
            <p><strong>₹<?= number_format($total, 2); ?></strong></p>
          </div>
        </div>
    <?php
      }
    } else {
      echo "<p>No items in your cart.</p>";
    }

    $discount = 0;
    $grandTotal = $subtotal - $discount;
    ?>

    <hr style="margin:20px 0;">

    <div class="order-total" style="font-size:16px;">
      <p>Discount: ₹<?= number_format($discount, 2); ?></p>
      <p><strong>Total: ₹<?= number_format($grandTotal, 2); ?></strong></p>
    </div>
  </div>
</div>

<style>
  .checkout-container {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    max-width: 1200px;
    margin: 40px auto;
    font-family: Arial, sans-serif;
  }

  .checkout-left,
  .checkout-right {
    flex: 1 1 45%;
    padding: 20px;
    background: #f7f7f7;
    border-radius: 8px;
  }

  .checkout-left h2,
  .checkout-right h2 {
    margin-bottom: 20px;
    color: #222;
  }

  .checkout-left form label {
    display: block;
    margin: 10px 0 5px;
  }

  .checkout-left form input,
  .checkout-left form textarea,
  .checkout-left form select {
    width: 100%;
    padding: 10px;
    margin-bottom: 15px;
    border-radius: 4px;
    border: 1px solid #ccc;
  }

  .checkout-btn {
    background: #2a4c9f;
    color: #fff;
    padding: 12px 20px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 16px;
  }

  .checkout-btn:hover {
    background: #1f3975;
  }

  .checkout-right .order-item {
    display: flex;
    align-items: center;
    margin-bottom: 15px;
  }

  .checkout-right .order-item img {
    width: 60px;
    height: 60px;
    object-fit: cover;
    margin-right: 15px;
    border-radius: 5px;
  }

  .checkout-right .order-item .order-details p {
    margin: 3px 0;
  }

  .checkout-right hr {
    margin: 15px 0;
  }

  .checkout-right p {
    margin: 5px 0;
    font-size: 14px;
  }

  .checkout-right strong {
    font-size: 16px;
    color: #222;
  }

  @media (max-width: 768px) {
    .checkout-left,
    .checkout-right {
      flex: 1 1 100%;
    }
  }
</style>

<?php include('include/footer.php'); ?>

Zerion Mini 1.0