Forum Replies Created

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • in reply to: ِA link to download dropdown menu items #37835
    Abbas
    Participant

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

    in reply to: ِA link to download dropdown menu items #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 6 days, 19 hours ago by Abbas.
    in reply to: Minimum and maximum selected checkboxes #37701
    Abbas
    Participant

    Hi Ally

    I modified it as you mentioned, and the code is now working properly

    I am grateful to you, and very happy with this support

    Thank you very much
    Abbas

    in reply to: Minimum and maximum selected checkboxes #37695
    Abbas
    Participant

    I simulated the code you put in to check the total goals and wrote the following code to check if between 4 and 6 options are selected:

    add_filter(‘quform_post_validate_141’, function (array $result, Quform_Form $form) {

    $checkboxIds = array(‘141_10’, ‘141_23’, ‘141_34’, ‘141_46’, ‘141_57’, ‘141_68’, ‘141_79’, ‘141_90’, ‘141_101’, ‘141_114’);

    $selectedCount = 0;

    foreach ($checkboxIds as $id) {
    $element = $form->getElement(“quform_$id”);

    if ($element instanceof Quform_Element_Checkbox && $element->isChecked()) {
    $selectedCount++;
    }
    }

    if ($selectedCount < 4 || $selectedCount > 6) {
    $result = array(
    ‘type’ => ‘error’,
    ‘error’ => array(
    ‘enabled’ => true,
    ‘title’ => ‘Error’,
    ‘content’ => ‘You must select between 4 and 6 options.’
    )
    );
    }

    return $result;
    }, 10, 2);

    but it appears to error:

    There was a problem
    Ajax Error

    Any advice?

    in reply to: Minimum and maximum selected checkboxes #37694
    Abbas
    Participant

    Update:

    Thee code snippet for the second requirement works properly.

    I made a small change to the code and it works for me.

    I changed:
    if ($total > 100)
    to
    if ($total != 100)

    And it works properly

    Very grateful to you

    Hope you help me restrict the number of options selected

    in reply to: Minimum and maximum selected checkboxes #37693
    Abbas
    Participant

    I also modified the code by changing the IDs, but unfortunately when I press the submit button the condition does not work and the total is not checked if it is equal to 100 or not

    This is the code I modified the IDs and added to Code Snippets:

    add_filter(‘quform_post_validate_141’, function (array $result, Quform_Form $form) {
    $ids = array(‘141_11’, ‘141_19’, ‘141_29’, ‘141_40’, ‘141_50′,’141_60′,’141_70′,’141_150’);
    $total = 0;

    foreach ($ids as $id) {
    $element = $form->getElement(“quform_$id”);

    if ($element instanceof Quform_Element_Field && !$element->isEmpty()) {
    $total += (int) $element->getValue();
    }
    }

    if ($total > 100) {
    $result = array(
    ‘type’ => ‘error’,
    ‘error’ => array(
    ‘enabled’ => true,
    ‘title’ => ”,
    ‘content’ => ‘Total goal importance ratio cannot exceed 100’
    )
    );
    }

    return $result;
    }, 10, 2);

    Any advice?

    in reply to: Minimum and maximum selected checkboxes #37692
    Abbas
    Participant

    First of all, I would like to thank you very much for your help, and I apologize for my poor English.

    It seems that the idea did not get across correctly.

    The code that you put works if the verification is works on one option box that contains several items, but in my form, each option box contains only one item, meaning that each option box has a different ID, which means that there is a need to create an array for verification according to my simple understanding.

    • This reply was modified 3 months, 3 weeks ago by Abbas.
    in reply to: Rename uploaded files #37631
    Abbas
    Participant

    Thanks a lot, the code works great.

Viewing 8 posts - 1 through 8 (of 8 total)
Be inspired. © 2025 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy