You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gravity Forms联动下拉框子字段填充失败求助(PHP实现)

Fixing Your Gravity Forms Chained Selects Sub-Field Issue

Hey there! Let's work through the kinks in your sub-field code and get those dynamic features loading correctly based on the selected company.

Key Issues in Your Current Code

  1. Incorrect Hook Parameter Order: The gform_chained_selects_input_choices hook has a strict parameter order that you didn't match—this is why your chain_value wasn't pulling the selected parent value properly.
  2. Mixed Return Values: You tried returning $items for all hooks, but gform_pre_render/gform_admin_pre_render need the modified $form, while gform_chained_selects_input_choices expects filtered input choices.
  3. No Company Filtering: Right now, you're loading all features from form 7 instead of only those linked to the selected parent company.
  4. Unnecessary Field Modification: For chained selects, sub-field options are handled directly via the gform_chained_selects_input_choices hook—you don't need to update the form's core field structure.

Corrected Code

// Handle form rendering hooks (no field changes needed here)
add_filter("gform_pre_render_14", "populate_features_render");
add_filter("gform_admin_pre_render_14", "populate_features_render");

// Handle dynamic choices for the sub-field (input 5.2 on form 14)
add_filter("gform_chained_selects_input_choices_14_5_2", "populate_features_choices", 10, 5);

// For pre-render hooks, just return the unmodified form
function populate_features_render($form) {
    return $form;
}

// Core logic to load features based on selected company
function populate_features_choices($input_choices, $form_id, $field, $input_id, $chain_value) {
    // Only target our specific form (14)
    if ($form_id != 14) {
        return $input_choices;
    }

    // Grab the selected company from the parent chain value
    $selected_company = $chain_value;
    if (empty($selected_company)) {
        return $input_choices; // Return empty if no company is selected yet
    }

    // Fetch entries from form 7 (where company data is stored)
    $entries = GFAPI::get_entries(7);
    if (!is_array($entries)) {
        return $input_choices;
    }

    $items = array();
    // Filter entries to only those matching the selected company (field 4 in form 7)
    foreach ($entries as $entry) {
        $entry_company = rgar($entry, '4');
        if ($entry_company === $selected_company) {
            $feature = rgar($entry, '10');
            // Only add non-empty features
            if (!empty($feature)) {
                $items[] = array(
                    'value' => $feature,
                    'text' => $feature,
                    'isSelected' => false
                );
            }
        }
    }

    // Remove duplicate features (in case multiple entries have the same feature for one company)
    $items = array_map("unserialize", array_unique(array_map("serialize", $items)));

    return $items;
}

What Changed & Why

  1. Split Hooks: We separated rendering hooks from the chained select logic to handle each correctly. The pre-render hooks just pass the form through since we don't need to modify its structure.
  2. Fixed Parameter Order: The populate_features_choices function uses the exact parameter order required by the gform_chained_selects_input_choices hook, so chain_value now correctly gets the selected parent company.
  3. Company-Specific Filtering: We now only pull features from entries in form 7 that match the selected company, instead of loading every feature in the system.
  4. Duplicate Removal: Added a quick check to eliminate duplicate features, which cleans up the dropdown if multiple entries list the same feature for a single company.
  5. Proper Return Values: Each hook returns the correct data type—$form for rendering, filtered choices for the chained select.

Quick Checks

  • Double-check that field IDs in form 7 are correct: field 4 = company name, field 10 = feature. Adjust these numbers if your actual fields differ.
  • If you want a default option (like "Select a feature...") when no company is selected, add it to $items before returning when $selected_company is empty.

内容的提问来源于stack exchange,提问作者Parkbum

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:54:02