ِA link to download dropdown menu items

Home Forums Quform WordPress ِA link to download dropdown menu items

This topic is: resolved
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #37829
    Abbas
    Participant

    Hello,

    Many of the forms I create using Quform include a dropdown list of school names. To keep track of which schools have completed the form, I previously asked for a way to hide schools that had already filled it out, and the code you provided worked wonderfully.

    ( https://gist.github.com/ThemeCatcher/c7dbe8e7598b013603d2ea3a2acbd77e )

    However, for ongoing tracking, I’d like a download link for a text file (the link can be placed next to the drop-down menu) or a page that displays the names of the remaining schools, with each school name on a new line.

    Regards,
    Abbas

    #37830
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

    #37831
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

    #37832
    Abbas
    Participant

    Hi

    Very cool… and I’m grateful.

    I modified the code so I can download the text file for any form.

    I tested the modified code and it works great. But, in your opinion, is it good and secure?

    Here’s the modified code:
    (Modified with the help of DeepSeek AI)

    add_action(‘init’, function () {
    // Check if the required parameters exist and Quform is active
    if (!isset($_GET[‘download_schools_txt’]) || !isset($_GET[‘id’]) || !class_exists(‘Quform’)) {
    return;
    }

    // Sanitize and validate the element ID (should be in format number_number e.g. 158_3)
    $id = sanitize_text_field($_GET[‘id’]);
    if (!preg_match(‘/^\d+_\d+$/’, $id)) {
    wp_die(‘Invalid element ID’, ‘Error’, array(‘response’ => 400));
    }

    // Extract form ID from the element ID
    list($form_id) = explode(‘_’, $id);

    // Get form configuration
    $config = quform(‘repository’)->getConfig($form_id);

    if (!is_array($config)) {
    wp_die(‘Form not found’, ‘Error’, array(‘response’ => 404));
    }

    // Create form instance
    $form = quform(‘formFactory’)->create($config);
    $select = $form->getElement(“quform_$id”);

    if (!$select instanceof Quform_Element_Select) {
    wp_die(‘Specified element is not a select dropdown’, ‘Error’, array(‘response’ => 400));
    }

    global $wpdb;

    // Query to get submitted values for this element
    $query = $wpdb->prepare(
    “SELECT ed.value
    FROM {$wpdb->prefix}quform_entry_data ed
    LEFT JOIN {$wpdb->prefix}quform_entries e
    ON ed.entry_id = e.id
    WHERE e.form_id = %d AND e.status = ‘normal’ AND ed.element_id = %d”,
    $form->getId(),
    $select->getId()
    );

    $submitted = $wpdb->get_col($query);
    $available_options = array();

    // Collect options that haven’t been submitted
    foreach ($select->getOptions() as $option) {
    if (isset($option[‘value’]) && !in_array($option[‘value’], $submitted, true)) {
    $available_options[] = $option[‘label’];
    }
    }

    // Generate file content
    $file_content = join(“\n”, $available_options);

    // Create filename with element ID and timestamp
    $filename = sprintf(‘available_options_%s_%d.txt’, $id, time());

    // Send HTTP headers for file download
    header(‘Content-Type: text/plain’);
    header(‘Content-Disposition: attachment; filename=”‘ . $filename . ‘”‘);
    header(‘Content-Length: ‘ . strlen($file_content));
    header(‘Pragma: no-cache’);
    header(‘Expires: 0’);

    // Output content and terminate
    echo $file_content;
    exit;
    });

    Regards,
    Abbas

    • This reply was modified 1 week, 1 day ago by Abbas.
    #37834
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

    #37835
    Abbas
    Participant

    Thank you so much.. I am grateful for you and the wonderful support

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Be inspired. © 2025 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy