Skip Navigation

[Resolved] Dynamically setting dropdown list value based on selected value from another lis

This support ticket is created 3 years, 6 months 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by Luo Yang 3 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#1815415
19102020.png

Refer to attached image.
The course state select field has been filled using wpt_field_options.

I'm wanting to now set the course name select field based on the value selected from course state field.

Is this possible?

#1815425

Thanks in advance...

#1815997

Hello,

Where do you want to display those fields as your screenshot?

If it is post form for creating/editing posts, then there isn't such kind of built-in feature, you might consider custom codes, for example:
hidden link

And according to our support policy, we don't provide custom codes support, you can also check it with our Toolset Contractors:
https://toolset.com/contractors/

#1818931

Hi,
Thanks for that link. I've been able to get a solution work but the part that I'm stuck on is pushing the data to a cred_generic_field that I have on a post form? Any thoughts on how I can inject the select options to a cred_generic_field on a post form where the first field is being selected?

Here is my code:

The first field that is selected.

add_filter( 'wpt_field_options', 'func_dynamic_populate', 10, 3);
function func_dynamic_populate($options, $title, $type ){
    global $wpdb;
    // if this is the social round nomination state field or the social round state field
    if ($title == 'social-round-nomination-state' || $title == 'social-round-state') {
            //$unique_state_values = array();
            $sql = $wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta pm, $wpdb->posts p WHERE p.post_status = 'publish' and meta_key  = 'wpcf-course-state' and pm.post_id=p.ID and p.post_type = 'golf-course-master' ORDER BY meta_value asc");
            $unique_state_values = $wpdb->get_results($sql);

            // Customise current options
            $options = array();
            foreach ($unique_state_values as $state) {
                $options[] = array(
                    '#value'    => $state->meta_value,
                    '#title'    => $state->meta_value); 
                }       
    }
    return $options;
    // add more if's here for any other dropdown fields.
}

Now when the value is selected, I've added the following code to display the data in a load-course DIV but as per above, I want it to inject the values into a cred_generic_field. Thoughts?

my cascadings-select.js

jQuery('select[name=social-round-nomination-state]').on('change', function($) {
    var state = jQuery(this).val();
    var data = {
        'action': 'get_courses_by_ajax',
        'state': state
    };

    jQuery.post(ajaxurl, data, function(response) {
        jQuery('.load-course').html(response);
    });
     
});

my functions.php

function cascade_select_script() {
    // Register the script
    wp_register_script( 'custom-script', get_stylesheet_directory_uri(). '/assets/js/cascading-select.js', array('jquery'), false, true );
  
    // Localize the script with new data
    $script_data_array = array(
        'ajaxurl' => admin_url( 'admin-ajax.php' ),
        'security' => wp_create_nonce( 'load_courses' ),
    );
    wp_localize_script( 'custom-script', 'blog', $script_data_array );
  
    // Enqueued script with localized data.
    wp_enqueue_script( 'custom-script' );
}
add_action( 'wp_enqueue_scripts', 'cascade_select_script' );
add_action('wp_ajax_get_courses_by_ajax', 'get_courses_by_ajax_callback');
add_action('wp_ajax_nopriv_get_courses_by_ajax', 'get_courses_by_ajax_callback');

function get_courses_by_ajax_callback(){
    $state = $_POST['state'];
    global $wpdb;
    
    $sql = $wpdb->prepare("SELECT distinct (select meta_value from $wpdb->postmeta pm1 where pm1.post_id = p.id and pm1.meta_key = 'wpcf-club-id') 'club_id',(select meta_value from $wpdb->postmeta pm2 where pm2.post_id = p.id and pm2.meta_key = 'wpcf-course-name') 'course_name' FROM $wpdb->postmeta pm, $wpdb->posts p WHERE p.post_status = 'publish' and pm.meta_key = 'wpcf-course-state' and pm.post_id=p.ID and p.post_type = 'golf-course-master' and pm.meta_value = %s ORDER BY course_name asc", $state);
    $state_courses = $wpdb->get_results($sql);

    if ( $state_courses ) {
        ?>
        <select>
            <?php
            foreach ($state_courses as $state_course) {
                ?>
                <option value="<?php echo $state_course->club_id; ?>"><?php echo $state_course->course_name; ?></option>
                <?php
            }
            ?>
        </select>
        <?php
    }
}
#1819651

There isn't such kind of feature within Toolset, in your case, you are using AJAX, in most cases, we output the AJAX result in JSON format, and use custom JS codes to output the result into HTML codes, please check the WP document:
https://codex.wordpress.org/AJAX_in_Plugins

And other JS documents:
hidden link

Here are some related posts about "Populate select from json"
https://stackoverflow.com/questions/16442422/jquery-populate-select-from-json

For your reference.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.