Use gform_pre_submission to update Upload Input

Hey there,
So I am using gform_pre_submission to create a csv out of the pre_entry data. That all is working fine. HOWEVER, when I then try to save the new link I’ve created to an uploadfile input, it wont save?

$_POST['input_74'] = $file_url;

I even try it with some JavaScript:

	?><script>
		document.querySelector("input#input_2_74").value = <?php echo $file_url ?>
		</script>
	<?php

So what gives? Here is the full code:

add_action( 'gform_pre_submission_2', 'gaws_pre_submission_handler' );
function gaws_pre_submission_handler( $form ) {
	$form_id = $form['id'];
	if( $form_id == 2 ){
		$upload_dir   = wp_upload_dir();
		$data = array();
		$csv = array(
			array(
				'Client ID',
				'Requestor Name (First Last)',
				'Requestor Email Address',
				'Administrator Name (First Last)',
				'Administrator Email Address',
				'Survey Project',
				'Client Organization',
				'Client Cohort',
				'Participant Name (First)',
				'Participant Name (Last)',
				'Participant Email Address',
				'Division',
				'Department',
				'Level',
				'Team',
				'Location',
				'Rater Name (First)',
				'Rater Name (Last)',
				'Rater Email Address',
				'Rater Group'
			)
		);
		
		$find[] = ',';
		$input_44 = str_replace( $find, '&#44;', rgpost( 'input_44' ) ); //Client ID
		$input_4 = str_replace( $find, '&#44;', rgpost( 'input_4' ) ); // Name (first last)/Requestor Name (First Last)
		$input_5 = str_replace( $find, '&#44;', rgpost( 'input_5' ) ); // Email Address/Requestor Email Address
		$input_7 = str_replace( $find, '&#44;', rgpost( 'input_7' ) ); // Name (first last)/Administrator Name (First Last)
		$input_8 = str_replace( $find, '&#44;', rgpost( 'input_8' ) ); // Email Address/Administrator Email Address
		$input_12 = str_replace( $find, '&#44;', rgpost( 'input_12' ) ); // Survey Project
		$input_13 = str_replace( $find, '&#44;', rgpost( 'input_13' ) ); // Client Organization
		$input_14 = str_replace( $find, '&#44;', rgpost( 'input_14' ) ); // Client Cohort
		$input_21 = str_replace( $find, '&#44;', rgpost( 'input_21' ) ); // First Name/Participant Name (First)
		$input_22 = str_replace( $find, '&#44;', rgpost( 'input_22' ) ); // Last Name/Participant Name (Last)
		$input_23 = str_replace( $find, '&#44;', rgpost( 'input_23' ) ); // Email Address/Participant Email Address
		$input_25 = str_replace( $find, '&#44;', rgpost( 'input_25' ) ); // Division
		$input_26 = str_replace( $find, '&#44;', rgpost( 'input_26' ) ); // Department
		$input_27 = str_replace( $find, '&#44;', rgpost( 'input_27' ) ); // Level
		$input_28 = str_replace( $find, '&#44;', rgpost( 'input_28' ) ); // Team
		$input_71 = str_replace( $find, '&#44;', rgpost( 'input_71' ) ); // Location
		$input_30 = str_replace( $find, '&#44;', rgpost( 'input_30' ) );

			array_push($csv, array($input_44, $input_4, $input_5, $input_7, $input_8, $input_12, $input_13, $input_14, $input_21, $input_22, $input_23, $input_25, $input_26, $input_27, $input_28, $input_71, $input_30[0], $input_30[1], $input_30[2], $input_30[3]) );
			
		//}
		
		$str = wp_generate_password( 6, false, false);
		$dir = 'wp-content/uploads/csv';
		if( ! is_dir ( $dir ) ){
			mkdir($dir);
		}
		$file_url = $dir . '/entry_'.$str.'.csv';

		
		$csv_handler = fopen ($file_url,'w');
		foreach ($csv as $fields) {
			fputcsv($csv_handler, $fields);
		}
		
		fclose ($csv_handler);
		
		$_POST['input_74'] = $file_url;
		?><script>
			document.querySelector("input#input_2_74").value = <?php echo $file_url ?>
			</script>
		<?php
	}
}

What do you mean? Where does this stop working for you?

At:

$_POST['input_74'] = $file_url;

The new entry does not include a populated uploadfile input/entry. Does it need to be something like:

$_POST['input_74']['url'] = $file_url;

???

That is correct to populate field 74 in the form with some value.

I recommend adding a ton of logging statements to your code, to see what the _POST is before you assign the value and after you assign the value. And any place you are curious what value you are trying assign to the _POST. You can use these approaches to logging:

You will need to be using the built in Gravity Forms logging feature:

1 Like

Thanks Chris… Huh… input 74 is populating here but not displaying in the Entry. ALSO, there are four other upload fields that aren’t displaying here

Array ( 
    [input_73] => 1 
    [input_44] => TESTY 
    [input_4] => Steven Tveter 
    [input_5] =>  
    [input_7] => 
    [input_8] => 
    [input_12] => Doing stuff 
    [input_13] => GoAdrift Web Solutions 
    [input_14] => 
    [input_21] => Steven 
    [input_22] => Tveter 
    [input_23] => 
    [input_25] => 
    [input_26] => 
    [input_27] => 
    [input_28] => 
    [input_71] => 
    [input_30] => Array ( 
        [0] => Steven 
        [1] => Tveter 
        [2] => 
        [3] => Internal Customers 
    ) 
    [input_15] => 02/20/2020 
    [input_16] => 03/12/2020 
    [input_61] => 02/25/2020 
    [input_63] => 03/09/2020 
    [input_62] => 03/13/2020 
    [input_69] => 
    [MAX_FILE_SIZE] => 134217728 
    [input_35] => Custom 
    [input_52] => Standard redaction 
    [input_37] => Custom 
    [g-recaptcha-response] => 03AOLTBLRfg4iB3SA1A628W8aRskfAc7sDS3GuImBc0QN1pNKBEqNcbZ_i0_iqAHJLuDYJ1Pk9_S7OL9tJDzaQd0wN62cP5R8EPUEkD4LmkHvYnaqL2X7kfGpoT0bXNkX0oAeOIZHrax5dkPYv9giwRx-2-Llp5Ku8qfs1KbS7e8icFiMZOtjHSUN-zeytqwqnHl1Wr_RS7qUmXr04BBvSbDAPEqCEgGB8TRbhDDBW6ffyOtKOp-jMHNDO3Su-evexssygV6uYV4iCDAWVuLX8pi3DJEE-L2OM0viS4cvYTVdy8qvue8pw9cC8HhpVq9hsi6qnGLWtsZc2pzFFR4XRLgyt0bpsjz_K8XW7dxnR5QnAyM-mOaFw7gIxmWN8TWM73_yOKntRZqr0 
    [input_53] => 
    [input_65] => true 
    [is_submit_2] => 1 
    [gform_submit] => 2 
    [gform_save] => 
    [gform_resume_token] => 
    [gform_unique_id] => 5e4b8196d90ac 
    [state_2] => WyJbXSIsIjJhYmI1MmQxMTE5M2ZiMTRkZjViOTU1MDk4NTZlNmU4Il0= 
    [gform_target_page_number_2] => 0 
    [gform_source_page_number_2] => 4 
    [gform_field_values] => 
    [input_74] => wp-content/uploads/csv/entry_3Vru3C.csv 
)

I am getting closer! I think I should have been adding the new csv file to $_POST[‘gform_uploaded_files’], which I now have done with this nifty code:

		$file = 'entry_'.$str.'.csv';
		$inputs    = GFCommon::json_decode(stripslashes(RGForms::post('gform_uploaded_files')));			
		$inputs['input_74'] = $file;
		$_POST['gform_uploaded_files'] = addslashes( GFCommon::json_encode($inputs));

BUT it still isn’t quite right. What am I missing in this process…