<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <link rel="stylesheet" href="bootstrap.min.css">
        <link rel="stylesheet" type="text/css" href="style.css">
        <title>Submitted</title>
    </head>
    <body>
        
        <?php
            require('config.php');
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
            $order = $_POST['order'];
            
            // Get the values of the select options and textarea
            $participant = $_POST['participant'];
            $campus = $_POST['campus'];
            $comments = mysqli_real_escape_string($conn, $_POST['comments']);
            //$comments = isset($_POST['comments']) ? $_POST['comments'] : '';
        
            // Separate the ordered items
            $orderedItems = explode(", ", $order);
        
            // Create an array for all items, initialized to NULL
            $itemValues = array_fill(1, 17, NULL);
        
            // Fill the array with ordered items
            foreach ($orderedItems as $order => $item) {
                $itemValues[$order + 1] = mysqli_real_escape_string($conn, $item);
            }
        
            // Check for non-selected items and mark them as "optional"
            for ($i = 1; $i <= 17; $i++) {
                if ($itemValues[$i] === NULL) {
                    $itemValues[$i] = "optional";
                }
            }
        
            // Build the SQL query to insert data into the columns
            $sql = "INSERT INTO responses (item_1, item_2, item_3, item_4, item_5, item_6, item_7, item_8, item_9, item_10, item_11, item_12, item_13, item_14, item_15, item_16, item_17, participant, campus, comments) VALUES ";
            $sql .= "('" . implode("', '", $itemValues) . "', '$participant', '$campus', '$comments')";
        
            if ($conn->query($sql) !== TRUE) {
                echo "Error: " . $sql . "<br>" . $conn->error;
            } else {
                // Display the image for 2 seconds
                        echo '<img class="submit-img" src="correct.gif" alt="Success" style="top: 25%;position: absolute;left: 33%;" />';
                        
                // Wait for 2 seconds (2000 milliseconds) before redirecting
                echo '<script>setTimeout(function() { window.location.href = "index.php"; }, 2000);</script>';
            }
        }
        
        $conn->close();
        ?>
        
    </body>
</html>
