简体   繁体   English

重力形成输入值

[英]Gravity forms input values

I'm new in the php world and i'm trying to build something in wordpress by gravity forms. 我是php世界的新手,我正在尝试通过引力形式在wordpress中构建一些东西。

Basicly for now I just playing around and try to create some codes I can use when I start building. 基本上,现在我只是在玩耍,尝试创建一些我可以在开始构建时使用的代码。 My question is: I have a form 1 where I can enter a name eg Football,Baseball,Hockey and it get saved to my data base. 我的问题是:我有一个表格1,可以在其中输入一个名称,例如Football,Baseball,Hockey,并将其保存到我的数据库中。 Then I have another form with the id 2 where there is a drop down box, here i want those values(names) submitted in form 1 to be dynamically populated. 然后我有另一个ID为2的表单,其中有一个下拉框,在这里我希望动态填充表单1中提交的那些值(名称)。 I have been trying to create a code by finding pieces around some websites (you will properly discover I'm mixing everything together) and ended up with this: 我一直在尝试通过在某些网站上查找片段来创建代码(您会发现我正在将所有内容混合在一起),最终得到了以下结果:

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

Hope someone can show me how it should be done so I can learn to do it in the future. 希望有人可以告诉我应该如何做,以便将来可以学习。

Sincerely 真诚的

Lars 拉尔斯

I'm afraid the $entry object is not available to the gform_pre_render , gform_pre_submission_filter or gform_admin_pre_render hooks. 恐怕$entry对象不适用于gform_pre_rendergform_pre_submission_filtergform_admin_pre_render挂钩。 Give the following a try. 请尝试以下方法。

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 );
}

The above is based on examples from the following sources: 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 以上内容基于以下来源的示例: http : //www.gravityhelp.com/documentation/page/Dynamically_Populating_Drop_Down_Fields http://www.gravityhelp.com/forums/topic/drop-down-dynamic-population-from-单行文字

I don't think you need to write any code at all. 我认为您根本不需要编写任何代码。 This is supported by Gravity Forms itself. 这由Gravity Forms本身支持。 In the form editor go to settings > confirmations, then choose redirect, enter the page form 2 is on and select Pass Field Data Via Query String then enter name={Name:1} or something similar depending on the name and id of the fields in Form 1. And then in Form 2, go to the field you want to populate and select advanced and dynamically prepopulate and enter what you typed to the left of the =, in this case name . 在表单编辑器中,转到“设置”>“确认”,然后选择“重定向”,进入页面表单2,然后选择“ Pass Field Data Via Query String然后输入name={Name:1}或类似的名称,具体取决于字段的名称和ID在Form 1中。然后在Form 2中,转到要填充的字段,然后选择“高级”和“动态预填充”,然后输入您在=左侧键入的内容,在本例中为name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM