Hi i need some help on how can i post selected variable from one dynamic dropdown to second one with ajax.
Here is my code and i need some help on how can i accomplish this.
Thank you.
//Dynamic category dropdown
add_filter("gform_pre_render_2", "monitor_single_dropdown");
function monitor_single_dropdown($form){
global $wpdb;
$nomos_table = $wpdb->prefix . 'nomous';
$wp_nomos_search = $wpdb->get_results("SELECT nomos_ID, nomos_Title FROM $nomos_table ORDER BY nomos_ID ASC");
//Creating drop down item array.
$items = array();
//Adding initial blank value.
//$items[] = array("text" => "", "value" => "");
//Adding dimos titles to the items array
foreach ( $wp_nomos_search as $nomostitle ){
$is_selected = $nomostitle->nomos_Title == "Test" ? true : false;
$items[] = array("value" => $nomostitle->nomos_ID, "text" => $nomostitle->nomos_Title, "isSelected"=> $is_selected);
}
//Adding other... item
//$items[] = array("text" => "Other...", "value" => "other");
//Adding items to field id 28
foreach($form["fields"] as &$field)
if($field["id"] == 28){
$field["type"] = "select";
$field["choices"] = $items;
}
?>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#input_2_28').change(function(){
var mainCat=jQuery('#input_2_28').val();
// call ajax
jQuery("#input_2_30").empty();
jQuery.ajax({
url:"<?php bloginfo( 'wpurl' ); ?>/wp-admin/admin-ajax.php",
type:'POST',
data:'action=monitor_children_dropdown&main_catid=' + mainCat,
success:function(results)
{
// alert(results);
// jQuery("#input_2_30").append(results);
}
});
}
);
});
</script>
<?php
return $form;
}
add_filter("gform_pre_render_2", "monitor_children_dropdown");
function monitor_children_dropdown($form) {
$parentCat=$_POST['main_catid'];
global $wpdb;
$dimoi_table = $wpdb->prefix . 'dimoi';
$wp_dimostitle_search = $wpdb->get_results("SELECT dimos_ID, dimos_Title FROM $dimoi_table WHERE nomos_ID=$parentCat ORDER BY dimos_ID ASC");
//Creating drop down item array.
$items = array();
//Adding initial blank value.
//$items[] = array("text" => "", "value" => "");
//Adding dimos titles to the items array
foreach ( $wp_dimostitle_search as $dimostitle ){
$is_selected = $dimostitle->dimos_Title == "Test" ? true : false;
$items[] = array("value" => $dimostitle->dimos_Title, "text" => $dimostitle->dimos_Title, "isSelected"=> $is_selected);
}
//Adding other... item
$items[] = array("text" => "Other...", "value" => "other");
//Adding items to field id 30
foreach($form["fields"] as &$field)
if($field["id"] == 30){
$field["type"] = "select";
$field["choices"] = $items;
}
return $form;
//return the result to the ajax post
die();
}
add_action( 'wp_ajax_nopriv_monitor_children_dropdown', 'monitor_children_dropdown'); //keep for people who allow post before registration
add_action( 'wp_ajax_monitor_children_dropdown', 'monitor_children_dropdown');