【问题标题】:Gravity forms input values重力形式输入值
【发布时间】:2014-05-27 13:14:28
【问题描述】:

我是 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;
}

希望有人能告诉我应该怎么做,这样我以后可以学会做。

真诚的

拉尔斯

【问题讨论】:

    标签: php wordpress gravity-forms-plugin


    【解决方案1】:

    恐怕$entry 对象不适用于gform_pre_rendergform_pre_submission_filtergform_admin_pre_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 );
    }
    

    以上内容基于以下来源的示例: http://www.gravityhelp.com/documentation/page/Dynamically_Populating_Drop_Down_Fields http://www.gravityhelp.com/forums/topic/drop-down-dynamic-population-from-single-line-text

    【讨论】:

      【解决方案2】:

      我认为您根本不需要编写任何代码。 Gravity Forms 本身支持这一点。在表单编辑器中,转到设置>确认,然后选择重定向,进入表单2打开的页面并选择Pass Field Data Via Query String,然后根据表单1中字段的名称和ID输入name={Name:1}或类似的东西。然后在表格 2,转到您要填充的字段并选择高级和动态预填充,然后在 = 左侧输入您键入的内容,在本例中为 name

      【讨论】:

        猜你喜欢
        • 2012-06-25
        • 2015-02-10
        • 2020-11-21
        • 2015-05-18
        • 1970-01-01
        • 2012-01-18
        • 2016-10-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多