Enter Picks

Go back

Your message has been sent

Warning

<?php
/**
 * Plugin Name: User Selection Form
 * Description: A plugin that allows users to submit their name and select 5 out of 64 checkbox options.
 * Version: 1.0
 * Author: Your Name
 */

// Shortcode to display the form
function user_selection_form() {
    ob_start();
    ?>
    <form method="post">
        <label for="user_name">Your Name:</label>
        <input type="text" name="user_name" required><br><br>
        
        <p>Select up to 5 options:</p>
        <?php
        $options = [];
        for ($i = 1; $i <= 64; $i++) {
            $options[] = "Option $i";
        }
        foreach ($options as $index => $option) {
            echo "<input type='checkbox' name='user_options[]' value='$option' class='checkbox-option'> $option <br>";
        }
        ?>
        <br>
        <button type="submit" name="submit_selection">Submit</button>
    </form>
    <script>
        document.querySelectorAll('.checkbox-option').forEach(box => {
            box.addEventListener('change', function () {
                let checkedBoxes = document.querySelectorAll('.checkbox-option:checked');
                if (checkedBoxes.length > 5) {
                    this.checked = false;
                    alert('You can only select up to 5 options.');
                }
            });
        });
    </script>
    <?php
    return ob_get_clean();
}
add_shortcode('user_selection_form', 'user_selection_form');

// Handle form submission
function handle_form_submission() {
    if (isset($_POST['submit_selection'])) {
        global $wpdb;
        $table_name = $wpdb->prefix . 'user_selections';
        $name = sanitize_text_field($_POST['user_name']);
        $options = isset($_POST['user_options']) ? array_slice($_POST['user_options'], 0, 5) : [];
        
        if (count($options) > 5) {
            wp_die('You can only select up to 5 options.');
        }

        $wpdb->insert($table_name, [
            'name' => $name,
            'selections' => maybe_serialize($options),
        ]);
        echo "<p>Thank you for your submission!</p>";
    }
}
add_action('init', 'handle_form_submission');

// Create table on plugin activation
function create_plugin_table() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'user_selections';
    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        name tinytext NOT NULL,
        selections text NOT NULL,
        PRIMARY KEY (id)
    ) $charset_collate;";

    require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    dbDelta($sql);
}
register_activation_hook(__FILE__, 'create_plugin_table');

// Admin panel menu to view submissions
function register_admin_menu() {
    add_menu_page('User Selections', 'User Selections', 'manage_options', 'user_selections', 'display_user_selections');
}
add_action('admin_menu', 'register_admin_menu');

function display_user_selections() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'user_selections';
    $results = $wpdb->get_results("SELECT * FROM $table_name");
    echo "<h2>User Submissions</h2>";
    echo "<table border='1'><tr><th>Name</th><th>Selections</th></tr>";
    foreach ($results as $row) {
        $selections = maybe_unserialize($row->selections);
        echo "<tr><td>{$row->name}</td><td>" . implode(', ', $selections) . "</td></tr>";
    }
    echo "</table>";
}