Php 重力形成输入值

Php 重力形成输入值,php,wordpress,gravity-forms-plugin,Php,Wordpress,Gravity Forms Plugin,我是php领域的新手,我正试图通过重力表单在wordpress中构建一些东西 基本上现在我只是在玩,尝试创建一些代码,我可以在开始构建时使用。我的问题是:我有一个表格1,我可以输入一个名字,例如足球、棒球、曲棍球,然后保存到我的数据库中。然后我有另一个id为2的表单,其中有一个下拉框,这里我希望动态填充在表单1中提交的值(名称)。我一直在尝试通过在一些网站上查找片段来创建代码(你会发现我将所有内容都混合在一起),结果是: add_filter("gform_pre_render", "test_

我是php领域的新手,我正试图通过重力表单在wordpress中构建一些东西

基本上现在我只是在玩,尝试创建一些代码,我可以在开始构建时使用。我的问题是:我有一个表格1,我可以输入一个名字,例如足球、棒球、曲棍球,然后保存到我的数据库中。然后我有另一个id为2的表单,其中有一个下拉框,这里我希望动态填充在表单1中提交的值(名称)。我一直在尝试通过在一些网站上查找片段来创建代码(你会发现我将所有内容都混合在一起),结果是:

add_filter("gform_pre_render", "test_task");

add_filter("gform_admin_pre_render", "test_task");

add_filter('gform_pre_submission_filter', 'test_task');
function test_task($entry, $form){

if($form["id"] != 2)
   return $form;

$entry["1"];

$items = array();

$items[] = array("text" => "Choose Sport", "value" => "");

foreach($posts as $post)
    $items[] = array("value" => $post->post_title, "text" => $post->post_title);

foreach($form["fields"] as &$field)
    if($field["id"] == 2){            
        $field["choices"] = $items;
    }

    return $form;
}
希望有人能告诉我应该怎么做,这样我就可以学会做它在未来

诚恳


Lars

恐怕
$entry
对象对于
gform\u pre\u render
gform\u pre\u submission\u filter
gform\u admin\u pre\u render
挂钩不可用。试试下面的方法

add_filter( 'gform_pre_render', 'populate_sports_choices' );
add_filter( 'gform_pre_validation', 'populate_sports_choices' );
add_filter( 'gform_pre_submission_filter', 'populate_sports_choices' );
add_filter( 'gform_admin_pre_render',  'populate_sports_choices' );
function populate_sports_choices( $form ) {
    // only run for form 2
    if( $form['id'] != 2 )
        return $form;

    foreach ( $form['fields'] as &$field ) {
function populate_sports_choices( $form ) {
    foreach ( $form['fields'] as &$field ) {

        // only populate the field if it is a select and has the designated css class name
        if ( $field['type'] != 'select' || strpos( $field['cssClass'], 'populate-sport' ) === false )
            continue;

        // get form 1 field 4 entry values
        $sports = get_entry_field_values( 4, 1 );

        // create the $choices array and set the placeholder choice
        $choices = array( array( 'text' => 'Select a Sport', 'value' => '' ) );

        // loop through each of the sports and add them to the $choices array
        foreach ( $sports as $sport ) {
            $choices[] = array( 'text' => $sport['value'], 'value' => $sport['value'] );
        }

        //replace the field choices with the contents of the $choices array
        $field['choices'] = $choices;

    }

    return $form;
}

/**
 * Allows you to retrieve an array of field values.
 * Requires either the $field object or a field ID and a form ID.
 *
 * Example: $values = get_entry_field_values( 5, 113 );
 */
function get_entry_field_values( $field_id, $form_id ) {
    global $wpdb;

    if ( is_array( $field_id ) ) {
        $field_id = rgget( 'id', $field_id );
    }

    $tablename = $wpdb->prefix . 'rg_lead_detail';
    $sql = "SELECT value FROM $tablename WHERE form_id = %d AND CAST(field_number as unsigned) = %d";

    return $wpdb->get_results( $wpdb->prepare( $sql, $form_id, $field_id ), ARRAY_A );
}
以上基于以下来源的示例:
恐怕
$entry
对象不可用于
gform\u pre\u呈现
gform\u pre\u提交过滤器
gform\u admin\u pre\u呈现
挂钩。试试下面的方法

add_filter( 'gform_pre_render', 'populate_sports_choices' );
add_filter( 'gform_pre_validation', 'populate_sports_choices' );
add_filter( 'gform_pre_submission_filter', 'populate_sports_choices' );
add_filter( 'gform_admin_pre_render',  'populate_sports_choices' );
function populate_sports_choices( $form ) {
    // only run for form 2
    if( $form['id'] != 2 )
        return $form;

    foreach ( $form['fields'] as &$field ) {
function populate_sports_choices( $form ) {
    foreach ( $form['fields'] as &$field ) {

        // only populate the field if it is a select and has the designated css class name
        if ( $field['type'] != 'select' || strpos( $field['cssClass'], 'populate-sport' ) === false )
            continue;

        // get form 1 field 4 entry values
        $sports = get_entry_field_values( 4, 1 );

        // create the $choices array and set the placeholder choice
        $choices = array( array( 'text' => 'Select a Sport', 'value' => '' ) );

        // loop through each of the sports and add them to the $choices array
        foreach ( $sports as $sport ) {
            $choices[] = array( 'text' => $sport['value'], 'value' => $sport['value'] );
        }

        //replace the field choices with the contents of the $choices array
        $field['choices'] = $choices;

    }

    return $form;
}

/**
 * Allows you to retrieve an array of field values.
 * Requires either the $field object or a field ID and a form ID.
 *
 * Example: $values = get_entry_field_values( 5, 113 );
 */
function get_entry_field_values( $field_id, $form_id ) {
    global $wpdb;

    if ( is_array( $field_id ) ) {
        $field_id = rgget( 'id', $field_id );
    }

    $tablename = $wpdb->prefix . 'rg_lead_detail';
    $sql = "SELECT value FROM $tablename WHERE form_id = %d AND CAST(field_number as unsigned) = %d";

    return $wpdb->get_results( $wpdb->prepare( $sql, $form_id, $field_id ), ARRAY_A );
}
以上基于以下来源的示例:
我认为您根本不需要编写任何代码。这是由重力形式本身支撑的。在表单编辑器中,转到设置>确认,然后选择重定向,进入表单2打开的页面,选择
通过查询字符串传递字段数据
,然后根据表单1中字段的名称和id输入
名称={name:1}
或类似内容。然后在表格2中,转到要填充的字段,选择advanced And Dynamics prepopulate,并在=,左侧输入您键入的内容,在本例中,
名称

我认为您根本不需要编写任何代码。这是由重力形式本身支撑的。在表单编辑器中,转到设置>确认,然后选择重定向,进入表单2打开的页面,选择
通过查询字符串传递字段数据
,然后根据表单1中字段的名称和id输入
名称={name:1}
或类似内容。然后在表格2中,转到要填充的字段,选择advanced和Dynamic prepopulate,并在=(在本例中为
name
)左侧输入您键入的内容