Forum Replies Created
- AuthorPosts
Abbas
ParticipantThank you so much.. I am grateful for you and the wonderful support
Abbas
ParticipantHi
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.
Abbas
ParticipantHi 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
AbbasAbbas
ParticipantI 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 ErrorAny advice?
Abbas
ParticipantUpdate:
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
Abbas
ParticipantI 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?
Abbas
ParticipantFirst 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.
Abbas
ParticipantThanks a lot, the code works great.
- This reply was modified 6 days, 19 hours ago by
- AuthorPosts