<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport"…
- User
header { background-color: #002f87; color: #fff; padding: 20px; text-align: center; } main { max-width: 800px; margin: 40px auto; background-color: #fff; padding: 30px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } h1 { margin-top: 0; color: #002f87; } fieldset { border: none; margin-bottom: 20px; } legend { font-weight: bold; margin-bottom: 10px; color: #002f87; } label { display: block; margin-top: 15px; } input[type="text"], input[type="email"], input[type="number"], textarea { width: 100%; padding: 10px; margin-top: 5px; border: 1px solid #ccc; border-radius: 4px; } textarea { resize: vertical; min-height: 100px; } button { background-color: #00a3e0; color: #fff; padding: 12px 25px; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; } button:hover { background-color: #008cc0; }<label for="address">Address:</label> <input type="text" id="address" name="address" required> <label for="contact">Contact Number:</label> <input type="text" id="contact" name="contact" required> <label for="email">Email Address:</label> <input type="email" id="email" name="email" required> </fieldset> <fieldset> <legend>Section 2: Claim Details</legend> <label for="euroducting_order">Euroducting Order Number:</label> <input type="text" id="euroducting_order" name="euroducting_order" required> <label for="customer_order">Customer Order Number:</label> <input type="text" id="customer_order" name="customer_order" required> <label for="delivery_note">Delivery Note:</label> <input type="text" id="delivery_note" name="delivery_note"> <label for="invoice">Invoice:</label> <input type="text" id="invoice" name="invoice"> <label for="item">Item:</label> <input type="text" id="item" name="item" required> <label for="quantity">Quantity:</label> <input type="number" id="quantity" name="quantity" required min="1"> <label for="description">Description of Product Defects:</label> <textarea id="description" name="description" required></textarea> <input type="hidden" id="remote_address" name="remote_address" readonly> </fieldset> <button type="submit">Submit Claim</button> </form>dodaj do tego skryptu obsługę dynamicznego dołączania plików do formularza i obsługę tego formularza po stronie serwera w php
- Assistant
Poniżej przedstawiam zmodyfikowany kod HTML z dodaną obsługą dynamicznego dołączania plików do formularza, oraz kod PHP po stronie serwera do obsługi tego formularza.
Zmodyfikowany HTML z obsługą dynamicznego dołączania plików:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Claim Form - Euroducting</title> <style> body { font-family: Arial, sans-serif; background-color: #f4f4f4; margin: 0; padding: 0; } header { background-color: #002f87; color: #fff; padding: 20px; text-align: center; } main { max-width: 800px; margin: 40px auto; background-color: #fff; padding: 30px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } h1 { margin-top: 0; color: #002f87; } fieldset { border: none; margin-bottom: 20px; } legend { font-weight: bold; margin-bottom: 10px; color: #002f87; } label { display: block; margin-top: 15px; } input[type="text"], input[type="email"], input[type="number"], textarea { width: 100%; padding: 10px; margin-top: 5px; border: 1px solid #ccc; border-radius: 4px; } textarea { resize: vertical; min-height: 100px; } button { background-color: #00a3e0; color: #fff; padding: 12px 25px; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; } button:hover { background-color: #008cc0; } .file-input-container { margin-top: 10px; } .remove-file-btn { background-color: #dc3545; color: #fff; border: none; padding: 5px 10px; font-size: 14px; cursor: pointer; margin-left: 10px; } .remove-file-btn:hover { background-color: #c82333; } </style> </head> <body> <header> <h1>Euroducting - Product Claim Form</h1> </header> <main> <form action="submit_claim.php" method="POST" enctype="multipart/form-data"> <fieldset> <legend>Section 1: Contact Information</legend> <label for="name">Name:</label> <input type="text" id="name" name="name" required> <label for="address">Address:</label> <input type="text" id="address" name="address" required> <label for="contact">Contact Number:</label> <input type="text" id="contact" name="contact" required> <label for="email">Email Address:</label> <input type="email" id="email" name="email" required> </fieldset> <fieldset> <legend>Section 2: Claim Details</legend> <label for="euroducting_order">Euroducting Order Number:</label> <input type="text" id="euroducting_order" name="euroducting_order" required> <label for="customer_order">Customer Order Number:</label> <input type="text" id="customer_order" name="customer_order" required> <label for="delivery_note">Delivery Note:</label> <input type="text" id="delivery_note" name="delivery_note"> <label for="invoice">Invoice:</label> <input type="text" id="invoice" name="invoice"> <label for="item">Item:</label> <input type="text" id="item" name="item" required> <label for="quantity">Quantity:</label> <input type="number" id="quantity" name="quantity" required min="1"> <label for="description">Description of Product Defects:</label> <textarea id="description" name="description" required></textarea> <label>Attachments:</label> <div id="file-inputs-container"> <div class="file-input-container"> <input type="file" name="attachments[]"> <button type="button" class="remove-file-btn" onclick="removeFileInput(this)">Remove</button> </div> </div> <button type="button" id="add-file-btn">Add Another File</button> <input type="hidden" id="remote_address" name="remote_address" readonly> </fieldset> <button type="submit">Submit Claim</button> </form> </main> <script> fetch('https://api.ipify.org?format=json') .then(response => response.json()) .then(data => { document.getElementById('remote_address').value = data.ip; }); document.getElementById('add-file-btn').addEventListener('click', addFileInput); function addFileInput() { const container = document.getElementById('file-inputs-container'); const newFileInput = document.createElement('div'); newFileInput.classList.add('file-input-container'); newFileInput.innerHTML = ` <input type="file" name="attachments[]"> <button type="button" class="remove-file-btn" onclick="removeFileInput(this)">Remove</button> `; container.appendChild(newFileInput); } function removeFileInput(btn) { const container = btn.parentNode; container.remove(); } </script> </body> </html>Kod PHP po stronie serwera (
submit_claim.php):<?php // Konfiguracja $uploadDir = 'uploads/'; // Katalog do przechowywania załączników $maxFileSize = 5 * 1024 * 1024; // 5MB $allowedFileTypes = ['image/jpeg', 'image/png', 'application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document']; // Funkcja do walidacji danych function validateData($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } // Sprawdzenie czy formularz został wysłany if ($_SERVER["REQUEST_METHOD"] == "POST") { // Pobranie danych z formularza $name = validateData($_POST['name']); $address = validateData($_POST['address']); $contact = validateData($_POST['contact']); $email = validateData($_POST['email']); $euroducting_order = validateData($_POST['euroducting_order']); $customer_order = validateData($_POST['customer_order']); $delivery_note = validateData($_POST['delivery_note']); $invoice = validateData($_POST['invoice']); $item = validateData($_POST['item']); $quantity = (int)$_POST['quantity']; $description = validateData($_POST['description']); $remote_address = validateData($_POST['remote_address']); // Walidacja podstawowych danych $errors = []; if (empty($name) || empty($address) || empty($contact) || empty($email) || empty($euroducting_order) || empty($customer_order) || empty($item) || $quantity < 1 || empty($description)) { $errors[] = "Wszystkie wymagane pola muszą być wypełnione."; } if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $errors[] = "Nieprawidłowy adres email."; } // Przetwarzanie załączników $attachments = []; if (isset($_FILES['attachments'])) { foreach ($_FILES['attachments']['name'] as $key => $filename) { if ($_FILES['attachments']['error'][$key] === 0) { $fileType = mime_content_type($_FILES['attachments']['tmp_name'][$key]); $fileSize = $_FILES['attachments']['size'][$key]; if (in_array($fileType, $allowedFileTypes) && $fileSize <= $maxFileSize) { $newFilename = uniqid() . '_' . basename($filename); $destination = $uploadDir . $newFilename; if (move_uploaded_file($_FILES['attachments']['tmp_name'][$key], $destination)) { $attachments[] = $newFilename; } else { $errors[] = "Nie udało się przesłać pliku: $filename"; } } else { $errors[] = "Plik $filename ma niedozwolony typ lub przekroczył dozwolony rozmiar (5MB)."; } } else { $errors[] = "Błąd podczas przesyłania pliku: $filename"; } } } // Zapisywanie danych do bazy lub pliku if (empty($errors)) { // Przykład z użyciem pliku tekstowego (do celów testowych) $logFile = 'claims.log'; $data = date('Y-m-d H:i:s') . "\n"; $data .= "IP: $remote_address\n"; $data .= "Imię i Nazwisko: $name\n"; $data .= "Adres: $address\n"; $data .= "Kontakt: $contact\n"; $data .= "Email: $email\n"; $data .= "Nr Zamówienia Euroducting: $euroducting_order\n"; $data .= "Nr Zamówienia Klienta: $customer_order\n"; $data .= "Nr Dowodu Dostawy: $delivery_note\n"; $data .= "Nr Faktury: $invoice\n"; $data .= "Produkt: $item\n"; $data .= "Ilość: $quantity\n"; $data .= "Opis wad: $description\n"; $data .= "Załączniki: " . implode(', ', $attachments) . "\n"; $data .= "------------------------\n"; file_put_contents($logFile, $data, FILE_APPEND); // Tutaj możesz dodać kod do zapisywania danych w bazie danych MySQL, np.: // $conn = new mysqli($servername, $username, $password, $dbname); // $sql = "INSERT INTO claims (...) VALUES (...)"; // $conn->query($sql); // $conn->close(); // Przekierowanie po sukcesie header('Location: thank_you.html'); exit; } else { // Wyświetlenie błędów echo '<h2>Wystąpiły błędy:</h2>'; echo '<ul>'; foreach ($errors as $error) { echo "<li>$error</li>"; } echo '</ul>'; echo '<a href="javascript:history.back()">Powrót do formularza</a>'; } } else { // Jeśli ktoś wejdzie bezpośrednio na submit_claim.php header('Location: index.html'); exit; }Uwagi:
- Bezpieczeństwo plików: W przykładzie ograniczono typy plików oraz ich rozmiar. Katalog
$uploadDirpowinien być niedostępny bezpośrednio z poziomu przeglądarki (użyj.htaccesslub konfiguracji serwera). - Baza danych: Kod PHP zawiera komentarz, gdzie powinieneś podmienić dane do połączenia z Twoją bazą MySQL i wykonać zapytanie
INSERTdo tabeliclaims. - Przechowywanie załączników: Katalog
uploads/musi istnieć i mieć prawa do zapisu dla serwera WWW (np.chmod 755 uploads/). - Dziękujemy: Utwórz plik
thank_you.htmlz podziękowaniem za wysłanie zgłoszenia.
Przykładowa struktura bazy danych MySQL (
claimstable):CREATE TABLE claims ( id INT AUTO_INCREMENT PRIMARY KEY, remote_address VARCHAR(45), name VARCHAR(255), address TEXT, contact VARCHAR(20), email VARCHAR(100), euroducting_order VARCHAR(50), customer_order VARCHAR(50), delivery_note VARCHAR(50), invoice VARCHAR(50), item VARCHAR(255), quantity INT, description TEXT, attachments TEXT, -- Przechowywanie nazw plików załączników oddzielonych przecinkami created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );W PHP zamiast logowania do pliku, wykonaj:
$conn = new mysqli($servername, $username, $password, $dbname); $sql = "INSERT INTO claims (remote_address, name, address, contact, email, euroducting_order, customer_order, delivery_note, invoice, item, quantity, description, attachments) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; $stmt = $conn->prepare($sql); $stmt->bind_param("ssssssssssiis", $remote_address, $name, $address, $contact, $email, $euroducting_order, $customer_order, $delivery_note, $invoice, $item, $quantity, $description, implode(',', $attachments) ); $stmt->execute(); $conn->close();Powyższy kod zapewni porządną obsługę formularza z załącznikami po stronie serwera. Pamiętaj o dostosowaniu danych dostępowych do bazy MySQL oraz o bezpieczeństwie serwera!
- Bezpieczeństwo plików: W przykładzie ograniczono typy plików oraz ich rozmiar. Katalog
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.