Request for Help: Issues with Saving and Retrieving Scores in Formidable Forms Plugin

By: Michele LaRocco | Asked: 07/02/2024
ForumsCategory: Code HelpRequest for Help: Issues with Saving and Retrieving Scores in Formidable Forms Plugin
Michele LaRoccoMichele LaRocco asked 3 weeks ago

Project Context: We are developing a custom WordPress plugin using Formidable Forms for a quiz that calculates and stores scores based on user selections.

Problem Statement: The calculated scores are not being saved in the hidden fields, and the retrieval of these scores is failing. Despite receiving a \"Your responses were successfully submitted. Thank you!\" message, the fields remain empty in the entries dashboard view. The error logs indicate that the scores are being calculated and attempted to be saved, but the retrieval of these scores is failing. Here are the key points from the logs:
  • Entry Processing: The function custom_calculate_scores is called and processes the entry correctly.
  • Panel Selections: The selections from each panel are logged correctly.
  • Score Calculation: The scores are calculated correctly before saving.
  • Score Saving: The scores are attempted to be saved, and the update results indicate success.
  • Score Retrieval: The retrieval of the saved scores fails, resulting in empty values
1 Answers
Rob LeVineRob LeVine answered 3 weeks ago
I'll say there's a lot more information that needs to be supplied to allow someone to help.  For example, there's no information about custom_calculate_scores.  Also, there's no mention of how you're putting the values into the hidden fields.  I can only assume that once the saving is corrected, the retrieval will fall into place.
Michele LaRoccoMichele LaRocco replied 3 weeks ago

Current Setup:

Form ID: 9
Hidden Fields for Scores:
total_A (mzmip)
total_R (gaj2e)
total_I (b52q1)
total_D (4bfn0)

Error Logs:
Here are some relevant error log entries:

[Tue Jul 02 18:07:56.605812 2024] [php:notice] [pid 3524] [client 172.98.33.67:0] custom_calculate_scores called. Entry ID: 41, Form ID: 9, referer: https://theartdnaquiz.com/wp-admin/admin-ajax.php?action=frm_forms_preview&form=artdna-quiz-short
[Tue Jul 02 18:07:56.605895 2024] [php:notice] [pid 3524] [client 172.98.33.67:0] Form ID matches. Processing entry..., referer: https://theartdnaquiz.com/wp-admin/admin-ajax.php?action=frm_forms_preview&form=artdna-quiz-short
[Tue Jul 02 18:07:56.607358 2024] [php:notice] [pid 3524] [client 172.98.33.67:0] Panel: panel1, Field Key: 7w12w, Selection: AAA, referer: https://theartdnaquiz.com/wp-admin/admin-ajax.php?action=frm_forms_preview&form=artdna-quiz-short
...
[Tue Jul 02 18:07:56.610819 2024] [php:notice] [pid 3524] [client 172.98.33.67:0] Scores before saving: A: 8, R: 8, I: 6, D: 6, referer: https://theartdnaquiz.com/wp-admin/admin-ajax.php?action=frm_forms_preview&form=artdna-quiz-short
[Tue Jul 02 18:07:56.614759 2024] [php:notice] [pid 3524] [client 172.98.33.67:0] Update result - total_A: 233, value: 8, referer: https://theartdnaquiz.com/wp-admin/admin-ajax.php?action=frm_forms_preview&form=artdna-quiz-short
...
[Tue Jul 02 18:07:56.626506 2024] [php:notice] [pid 3524] [client 172.98.33.67:0] Retrieved Scores - total_A: , referer: https://theartdnaquiz.com/wp-admin/admin-ajax.php?action=frm_forms_preview&form=artdna-quiz-short
...

add_action('frm_after_create_entry', 'custom_calculate_scores', 20, 2);

function custom_calculate_scores($entry_id, $form_id) {
error_log("custom_calculate_scores called. Entry ID: $entry_id, Form ID: $form_id");

if ($form_id == 9) { // Replace 9 with your actual Form ID
error_log("Form ID matches. Processing entry...");

if (!class_exists('FrmEntryMeta')) {
error_log("FrmEntryMeta class not found.");
return;
}

// Define the attribute codes directly
$image_codes = array(
'AAA' => array('A' => 4, 'R' => 0, 'I' => 0, 'D' => 0),
// Additional codes omitted for brevity
);

// Define the fields for each panel
$fields = array(
'panel1' => '7w12w', // Field key for Panel 1
'panel2' => '771d8', // Field key for Panel 2
'panel3' => 'aeazn', // Field key for Panel 3
'panel4' => '4yt6k', // Field key for Panel 4
'panel5' => 'g3gxf', // Field key for Panel 5
// Add more panels as needed
);

$scores = array('A' => 0, 'R' => 0, 'I' => 0, 'D' => 0);

// Calculate scores based on selected images
foreach ($fields as $panel => $field_key) {
// Get the selected image code for the current panel
$selection = FrmEntryMeta::get_entry_meta_by_field($entry_id, $field_key, true);
error_log("Panel: $panel, Field Key: $field_key, Selection: $selection");

if ($selection && isset($image_codes[$selection])) {
// Get the attribute code for the selected image
$code = $image_codes[$selection];
// Update scores based on the attribute code
foreach ($code as $key => $value) {
$scores[$key] += $value;
}
}
}

error_log("Scores before saving: A: {$scores['A']}, R: {$scores['R']}, I: {$scores['I']}, D: {$scores['D']}");

// Save the scores back to hidden fields
FrmEntryMeta::add_entry_meta($entry_id, 'mzmip', null, $scores['A']); // total_A
FrmEntryMeta::add_entry_meta($entry_id, 'gaj2e', null, $scores['R']); // total_R
FrmEntryMeta::add_entry_meta($entry_id, 'b52q1', null, $scores['I']); // total_I
FrmEntryMeta::add_entry_meta($entry_id, '4bfn0', null, $scores['D']); // total_D

// Check the update results again
$total_A = FrmEntryMeta::get_entry_meta_by_field($entry_id, 'mzmip', true);
$total_R = FrmEntryMeta::get_entry_meta_by_field($entry_id, 'gaj2e', true);
$total_I = FrmEntryMeta::get_entry_meta_by_field($entry_id, 'b52q1', true);
$total_D = FrmEntryMeta::get_entry_meta_by_field($entry_id, '4bfn0', true);

error_log("Retrieved Scores - total_A: $total_A, total_R: $total_R, total_I: $total_I, total_D: $total_D");
}
}

Rob LeVineRob LeVine replied 3 weeks ago

I suggest checking the value returned by "FrmEntryMeta::add_entry_meta" and outputting it to error_log. There are many reasons it could fail: 1. It will fail if the meta_value already exists (in which case you need to use update_entry_meta and 2. if the field key is wrong. At first I thought it was because you were sending the key instead of the id, but it seems like both add_entry_meta and get_entry_meta_by_field will eventually convert it. Even though that's true you might try using FrmField::get_id_by_key('mzmip') where you currently have 'mzmip' and seeing if that helps.

Michele LaRoccoMichele LaRocco replied 3 weeks ago

thanks will be updating and checking this shortly!! really appreciate you taking the time!

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right