Accessing checked form field data before submission [RESOLVED]

I have a multi page form. On page 2, the user selects 10 options from a list of 20 checkbox options. On page 3, I need to copy each of the selected options into it’s own single line text field. I think I need to access the data from POST but not sure how to do this and cannot find anything online. I only want to access the checked options.

1 Like

Will this work? And will only checked options be stored?

$selected_values = array();

if ( $current_page == 4 ) {
    for ($i = 1; $i < 48 ; $i++){
    	$selected_values[$i] = rgpost( 'input_307.' . $i, true );
    
    }

Hi @andrenellin - welcome to the forum :slight_smile:

I have solved this use case myself, and created a plugin to make your requirement more simplified.

In essence, you give your checkbox field an Admin Label (eg. grp1_soup_menu), and upon the user’s action with “previous/next/submit” buttons, the entry values will be populated into the global sessions array under the key “grp1_soup_menu”.

If you are interested in trying this approach, see the docs on GitHub:

Cheers and good luck !
-JAS

Thanks JAS. Would you mind giving an example of how, for a logged in user, where data is being stored to the WordPress database, I could retrieve the data for options selected on page 2, to use on page 3, of a multi page form.

Sure thing @andrenellin !

I should have asked if you were using the “user logged in context,” as that does make a difference (as you prolly saw mentioned in the docs).

You can see an example of acquiring the user’s meta in the function:
mfsfs_load_user_meta() in shared-fields.php file.

The crucial lines are:

define('MFSFS_ID', 'mfsfs_SYS_'); // (use for referencing with your Admin Label)
$current_user = wp_get_current_user();
$GLO_hold_user_meta = array_map(function ($a) { return $a[0]; },
    get_user_meta($current_user->ID));

So you could see all that gets retrieved in your log file with:

$tmp = print_r($GLO_hold_user_meta, TRUE);
GFCommon::log_debug("mfsfs_save_fields(3)" . ' User Meta Ary:' . $tmp);

Using in your code would look like:

$info = $GLO_hold_user_meta[ MFSFS_ID . 'grp1_soup_menu' ];

(note: the docs explain about normalizing the key, as performed by function mfsfs_convert_str_to_wp_fmt)

It’s been a while for me, but IIRC the value for $info (choices array) is probably in a serialized array format, it should be obvious once your view the debug log info - I would recommend viewing that. You don’t even need to log anything in your code, just set the form’s debug level to “noisy,” and search for this pattern: “mfsfs_load_fields(3): user ID=” it will have dumped out all the user’s retrieved meta data.

I can see I need to put more work into my “PHP Dev’s Guide” section :slight_smile:
Thanks for your feedback !

1 Like

You can use mergetags in multipage forms. Just use {checkboxfield:2.1}, {checkboxfield:2.2} etc in the default value property of the text fields.

I used the following solution:

$selected_values = [];
// Variable to store the definitions of the selected values

// GET SELECTED CHOICES
$current_page = GFFormDisplay::get_current_page($form['id']);

if ($current_page == 3) {
	for ($i = 1; $i < 50; $i++) {

		// Get Values with Definitions
		$getValue = rgpost("input_307_" . $i, true);

		// Store values in separate array
		$value = substr($getValue, 0, $position);
		if (strlen($value) > 0) {
			array_push($selected_values, $value);
		}
	}
1 Like