Help please with php

hi there,

i’m trying to populate a list field with values via php, after form submission.

At this stage my list field is being updated with blank rows only..

can you take a look at my code please?

add_action('gform_after_submission', 'populate_list_field_after_submission', 10, 2);
function populate_list_field_after_submission($entry, $form) {
    // Check if the form ID matches the form you want to target
    if ($form['id'] == 18) { // Replace 1 with your form ID
        // Get the entry ID
        $entry_id = $entry['id'];

        // Define the list field ID you want to populate
        $list_field_id = 1; // Replace 2 with your list field ID

        // Define the data you want to populate into the list field
        $list_data = array(
            array('Column 1', 'Column 2', 'Column 3'), // Replace with your actual column names
            array('Value 1', 'Value 2', 'Value 3'),    // Replace with your actual values
            array('Value 4', 'Value 5', 'Value 6')
        );
 // Convert the list data to JSON format
        $list_data_json = json_encode($list_data);

       
        // Update the entry with the new list field data
        GFAPI::update_entry_field($entry_id, $list_field_id, $list_data_json);
    }
}


The list field doesn’t use JSON for the entry value:

You’ll need to use serialize() instead of json_encode(), also the array is wrong, check the docs page above for the correct way it should be organized.

Im not an experienced coder. can you please assist? i cant make much sense of the docs

Try the following:

add_action( 'gform_after_submission', 'populate_list_field_after_submission', 10, 2 );
function populate_list_field_after_submission( $entry, $form ) {
	// Check if the form ID matches the form you want to target
	if ( $form['id'] == 18 ) { // Replace 1 with your form ID
		// Get the entry ID
		$entry_id = $entry['id'];

		// Define the list field ID you want to populate
		$list_field_id = 1; // Replace 2 with your list field ID

		// Define the data you want to populate into the list field
		$list_data = array(
			array(
				'Column 1' => 'Value 1',
				'Column 2' => 'Value 2',
				'Column 3' => 'Value 3',
			),
			array(
				'Column 1' => 'Value 4',
				'Column 2' => 'Value 5',
				'Column 3' => 'Value 6',
			),
		);

		// Update the entry with the new list field data
		GFAPI::update_entry_field( $entry_id, $list_field_id, serialize( $list_data ) );
	}
}