Horje
Gravity Forms - Populate Checkbox in Form from Multiple Single Line Text Input Answers from User - Wordpress Solution
Hello, This is a Gravity Forms question/problem. I have multi-page form where I need checkboxes later in the form to be populated with the user's answers from single line text inputs earlier in the form. To simplify it: // Page One Inputs Single Line Text Input : input_1 Single Line Text Input : input_2 Single Line Text Input : input _3 Single Line Text Input : input _4 // Then on another page in the form Checkbox Field 1 option 1 : input_1 option 2 : input_2 option 3 : input_3 option 4 : input_4 // Another page in the form Checkbox Field 2 option 1 : input_1 option 2 : input_2 option 3 : input_3 option 4 : input_4 The tricky part is that the checkbox is built from MULTIPLE single line text inputs, so it's many-to-one in its relationship. Thanks!

Solution - 1

Try this, note there are some IDs you need to update for your form

add_filter( 'gform_pre_render_1', 'my_populate_checkbox' );//Change the 1 to your form ID
function my_populate_checkbox( $form ) {
 
     $choices = array();
	 
	 $single_text_ids = array( 1, 2, 3, 4); //text input IDs
	 
	 foreach( $single_text_ids as $id ){
	 
		$choices[] = ('text' => rgpost( 'input_1_'.$id  ), 'value' => rgpost( 'input_1_'.$id ) ); //The ones need to be form ID
		
	}

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

    if( 1 === $field->id ) { //Change to your checkbox ID
 
      $field['choices'] = $choices;
  
   } 
  
  } 

  return $form;
}


Solution - 2

You will have to prepopulate the fields in your other pages. This way you can achieve as well.. jQuery is another method..


Solution - 3

You can add the filter code to populate the checkbox options from submitted fields, please replace the form id and field ids:


// replace 1 with the form id
add_filter( 'gform_pre_render_1', 'populate_checkboxes' );
function populate_checkboxes( $form ) {
	// get the current page
	$current_page = GFFormDisplay::get_current_page( $form['id'] );

	// replace 2 with the page where the first checkbox field will show
	if ( $current_page == 2 ) {
		foreach ( $form['fields'] as &$field ) {
			//replace 5 with the id of the first checkbox field
			if ( $field->id == 5 ) {
				$field['choices'] = array();
				$field['choices'][] = array( 'text' => $_POST['input_1'], 'value' => $_POST['input_1'] );
				$field['choices'][] = array( 'text' => $_POST['input_2'], 'value' => $_POST['input_2'] );
				$field['choices'][] = array( 'text' => $_POST['input_3'], 'value' => $_POST['input_3'] );
				$field['choices'][] = array( 'text' => $_POST['input_4'], 'value' => $_POST['input_4'] );
				break;
			}
		}
	}

	// replace 3 with the page where the second checkbox field will show
	if ( $current_page == 3 ) {
		foreach ( $form['fields'] as &$field ) {
			//replace 6 with the id of the second checkbox field
			if ( $field->id == 6 ) {
				$field['choices'] = array();
				$field['choices'][] = array( 'text' => $_POST['input_1'], 'value' => $_POST['input_1'] );
				$field['choices'][] = array( 'text' => $_POST['input_2'], 'value' => $_POST['input_2'] );
				$field['choices'][] = array( 'text' => $_POST['input_3'], 'value' => $_POST['input_3'] );
				$field['choices'][] = array( 'text' => $_POST['input_4'], 'value' => $_POST['input_4'] );
				break;
			}
		}
	}

	return $form;
}
I haven't tested the code but I think it'll work. Let me know if you have any problem.





Wordpress

Related
Force DIV refresh upon coupon X application / removal - Wordpress Solution Force DIV refresh upon coupon X application / removal - Wordpress Solution
looking for a good way to query posts on a list of specific days. tryi - Wordpress Solution looking for a good way to query posts on a list of specific days. tryi - Wordpress Solution
Custom SQL Query? - Wordpress Solution Custom SQL Query? - Wordpress Solution
Plugin User Role Level Error - Wordpress Solution Plugin User Role Level Error - Wordpress Solution
Walk array calculate shift - Wordpress Solution Walk array calculate shift - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
12