Skip Navigation

[Resolved] post form with custom fields conditional select option

This support ticket is created 6 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by rexJ-2 6 years ago.

Assisted by: Nigel.

Author
Posts
#1168598
Skærmbillede 2018-12-18 kl. 11.12.02.png
Skærmbillede 2018-12-18 kl. 11.14.08.png

Tell us what you are trying to do?
have a form with a select field where i need to control which options is visible.
the Select options must be conditional set so if a condition for a select option is false this option will not be in the select box
Can i do it in toolset or in php?
toolset:

 <div class="form-group">
		<label>begivenhedstype</label>
		[cred_field field="begivenhedstype" force_type="field" class="form-control" output="bootstrap"]
</div>

php:

 
add_action('cred_save_data', 'after_save_data_for_form',30,2);
function after_save_data_for_form($post_id, $form_data)
{
    //some code here
    switch($form_data['id']) {
    case 236:
    	global $wpdb;  	
		 //some code here
        break;
    default:
        //code to be executed if n is different from all labels;
    
	}
}

Is there any documentation that you are following?
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/checking-types-fields-and-custom-fields/
https://toolset.com/documentation/customizing-sites-using-php/functions/#select
https://toolset.com/documentation/user-guides/conditional-display-for-form-inputs/

Is there a similar example that we can see?
i do not have a working example but would like to use the php example above with form id 236 if possible
What is the link to your site?

#1168686

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

There are no API filters available where you can modify the options included in a select field inserted in a form.

The only option would be to use JavaScript to remove the options you don't want to include in the browser.

The question then is how is it determined which options should be removed?

If there is some test that you perform when the page is being built on the server then you would need to add some PHP that checked you were on the right page, performed your test, then created an array of the options that should be excluded, then made that same array available on the front end using the WP function wp_localize_script, which your custom JS would then use to modify the select input in the DOM, removing the unwanted options.

Here is the documentation for wp_localize_script (https://developer.wordpress.org/reference/functions/wp_localize_script/) together with a more user-friendly guide (hidden link).

So you will need a custom PHP script and a custom JS script.

#1170519

I understand its not an API filter available in toolset, so i look in wordpress forum.
For others maybe search for
Html5 select

<div id="selectchange">
  <select id="sel">
    <option>Cat</option>
    <option>Dog</option>
    <option>Fish</option>
</select>

</div>

javascript + jquery

// find elements
var find = $('#sel');
find.empty().append('<option value="">---Select ---</option>');
find.append('<option value="">---appended ---</option>');
    d3.select('#sel').property('value', 'chair');