connect_error) { die("Connection failed: " . $conn->connect_error); } // Retrieve form data $name = $_POST['name']; $email = $_POST['email']; $category = $_POST['category']; // e.g., 'Volwassenen Vrijdag' or 'Kinderen Zaterdag' $quantity = (int)$_POST['quantity']; // Extract day and category from the input list($categoryType, $day) = explode(' ', $category); // Split into 'Volwassenen'/'Kinderen' and 'Vrijdag'/'Zaterdag' $day = strtolower($day) === 'vrijdag' ? 'friday' : 'saturday'; $categoryType = strtolower($categoryType) === 'volwassenen' ? 'volwassen' : 'kind'; require __DIR__ . '/../phpmailer/src/PHPMailer.php'; require __DIR__ . '/../phpmailer/src/SMTP.php'; require __DIR__ . '/../phpmailer/src/Exception.php'; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; // Prepare SQL statement $sql = "INSERT INTO tickets (ticket_id, name, email, category, day, quantity, qr_code_link) VALUES (?, ?, ?, ?, ?, ?, ?)"; $stmt = $conn->prepare($sql); if (!$stmt) { die("Error preparing statement: " . $conn->error); } $qrCodes = []; // Array to store QR code URLs for email attachment // Insert one ticket per row and generate QR codes for ($i = 0; $i < $quantity; $i++) { $ticket_id = bin2hex(random_bytes(16)); // Generate a 32-character unique ticket ID $qrCodeUrl = "https://api.qrserver.com/v1/create-qr-code/?size=500x500&data=" . urlencode($ticket_id); // Generate QR code link $one_ticket_quantity = 1; // Each row represents one ticket $stmt->bind_param("sssssis", $ticket_id, $name, $email, $categoryType, $day, $one_ticket_quantity, $qrCodeUrl); if (!$stmt->execute()) { echo "Error: " . $stmt->error; $stmt->close(); $conn->close(); exit(); } $qrCodes[] = $qrCodeUrl; // Store QR code URL for email } $stmt->close(); $conn->close(); // Send confirmation email with QR codes $mail = new PHPMailer(true); try { $mail->isSMTP(); $mail->Host = 'smtp.gmail.com'; // Gmail SMTP server $mail->SMTPAuth = true; $mail->Username = 'ticketsopdracht@gmail.com'; // Your Gmail address $mail->Password = 'rqxm fbju xbmu qmbr'; // Your App Password from Google $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Use TLS encryption $mail->Port = 587; // TLS port $mail->setFrom('ticketsopdracht@gmail.com', 'Spik & Span'); // Sender email and name $mail->addAddress($email, $name); // Recipient email and name $mail->isHTML(true); $mail->Subject = 'Bevestiging van je bestelling'; // Build the email body with inline styles $mail->Body = '

Bedankt voor je bestelling!

Beste ' . htmlspecialchars($name) . ',

We hebben je bestelling succesvol ontvangen. Hieronder vind je de details van je bestelling:

Naam: ' . htmlspecialchars($name) . '
E-mail: ' . htmlspecialchars($email) . '
Categorie: ' . htmlspecialchars($category) . '
Aantal Tickets: ' . $quantity . '

Hieronder vind je de QR-codes voor je tickets:

'; // Add QR codes to the email body foreach ($qrCodes as $index => $qrCodeUrl) { $mail->Body .= '

Ticket ' . ($index + 1) . ':

QR Code
'; } $mail->Body .= '

We wensen je veel plezier tijdens het evenement!

Met vriendelijke groet,
Spik & Span

'; $mail->send(); // Redirect to the order completion page header("Location: ../order-complete.html"); exit(); } catch (Exception $e) { echo "Email could not be sent. Mailer Error: {$mail->ErrorInfo}"; } ?>