我是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;
}
希望有人能告诉我应该怎么做,这样我才能学会将来这样做。
此致
拉斯
答案 0 :(得分:2)
我担心$entry
,gform_pre_render
或gform_pre_submission_filter
挂钩无法使用gform_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
答案 1 :(得分:2)
我认为你根本不需要编写任何代码。这是由Gravity Forms本身支持的。在表单编辑器中转到设置>确认,然后选择重定向,输入页面2,然后选择Pass Field Data Via Query String
,然后输入name={Name:1}
或类似的内容,具体取决于表单1中字段的名称和ID。然后在表单2中,转到在您要填充的字段中,选择高级并动态预填充,然后输入您在=左侧输入的内容,在本例中为name
。