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
}
}