Support » Plugin: Ninja Forms - The Easy and Powerful Forms Builder » Prepopulate a listselect field with a query-string value

  • Resolved speedypete

    (@speedypete)


    In the old core version it was very easy to write a small code-snippet to alter the default value of a listselect field depending on a query variable.
    I don’t know how to do it with the new version.
    I tried most of the hooks and filters…
    Can pls somebody give me a hint?

    Thank you!

    Regards, Pete

Viewing 4 replies - 1 through 4 (of 4 total)
  • This is my code:

    I have a query-string value which defines wether a person is a “Junior” or a “Senior”. In the form i have a listselect field (id=60) for that information and i am trying to preselect the apropriate option according to the query-string value before the form is schowing.

    add_filter( ‘ninja_forms_display_before_form’,’my_list’, 10, 2 );

    function my_list( $form_id, $is_preview){
    $js=$_GET[‘js’];
    $field = Ninja_Forms()->form()->get_field( 60 );
    $ary=$field->get_setting(‘options’);
    foreach($ary AS $name) {
    If ($js==”Senior”){
    If ($name[‘label’]==”Senior”){$name[‘selected’]=1;}
    If ($name[‘label’]==”Junior”){$name[‘selected’]=0;}
    }
    If ($js==”Junior”){
    If ($name[‘label’]==”Senior”){$name[‘selected’]=0;}
    If ($name[‘label’]==”Junior”){$name[‘selected’]=1;}
    }
    }
    $field->update_setting( ‘options’, $ary )->save();
    }

    So, i think this is better readable:

    
    add_filter( 'ninja_forms_display_before_form','my_list', 10, 2 );
    
    function my_list( $form_id, $is_preview){
    	
    	$js=$_GET['js'];
    	$field = Ninja_Forms()->form()->get_field( 60 );
    	$ary=$field->get_setting('options');
    	foreach($ary AS $name) {
    		If ($js=="Senior"){
    			If ($name['label']=="Junior"){$name['selected']=0;}
    			If ($name['label']=="Senior"){$name['selected']=1;}
    		}
    		If ($js=="Junior"){
    			If ($name['label']=="Senior"){$name['selected']=0;}
    			If ($name['label']=="Junior"){$name['selected']=1;}
    		}		
    	}
            $field->update_setting( 'options', $ary )->save();
    }
    
    Plugin Author Kyle B. Johnson

    (@kbjohnson90)

    With THREE, you can use the field key in the query string to pre-select a value for lists.

    ie site.com/?my_list=two

    You can set a field’s key in the field’s settings drawer, under the administration section.

    Solved, thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Prepopulate a listselect field with a query-string value’ is closed to new replies.