Guest User

Gravity Form Dynamic ajax category + child dropdown

a guest
May 25th, 2012
1,567
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Dynamic category dropdown
  2. add_filter("gform_pre_render_2", "monitor_single_dropdown");
  3. function monitor_single_dropdown($form){
  4.  
  5.     global $wpdb;
  6.     $nomos_table = $wpdb->prefix . 'nomous';
  7.     $wp_nomos_search = $wpdb->get_results("SELECT nomos_ID, nomos_Title FROM $nomos_table ORDER BY nomos_ID ASC");
  8.  
  9.     //Creating drop down item array.
  10.     $items = array();
  11.  
  12.     //Adding nomos to the items array
  13.     foreach ( $wp_nomos_search as $nomos ){
  14.     $is_selected = $nomos->nomos_Title == "Θεσσαλονίκης" ? true : false;
  15.     $items[] = array("value" => $nomos->nomos_ID, "text" => $nomos->nomos_Title, "isSelected"=> $is_selected);
  16.     }
  17.  
  18.     //Adding other... item
  19.     $items[] = array("text" => "Other...", "value" => "0");
  20.  
  21.      //Adding items to field id 28
  22.         foreach($form["fields"] as &$field)
  23.  
  24.             if($field["id"] == 28){
  25.                 $field["type"] = "select";
  26.                 $field["choices"] = $items;
  27. }
  28.    
  29. ?>
  30. <script type="text/javascript">
  31.  
  32. jQuery(document).ready(function(){
  33.    
  34.     jQuery('#input_2_28').change(function(){
  35.             var mainCat=jQuery('#input_2_28').val();
  36.  
  37.             // call ajax
  38.              jQuery("#input_2_31").empty();
  39.                 jQuery.ajax({
  40.                     url:"<?php bloginfo( 'wpurl' ); ?>/wp-admin/admin-ajax.php",
  41.                     type:'POST',
  42.                     data:'action=monitor_children_dropdown&main_catid=' + mainCat,
  43.  
  44.                      success:function(results)
  45.                          {
  46.             //  alert(results);
  47.                jQuery("#input_2_31").append(results);
  48.                                 }
  49.                            });
  50.                   }
  51.             ); 
  52. });
  53.  
  54. </script>
  55.    
  56. <?php
  57. return $form;
  58. }
  59.  
  60. //ajax callback function
  61. function monitor_children_dropdown() {
  62. if(isset($_POST['main_catid'])) {
  63.    
  64. $parentCat=$_POST['main_catid'];
  65. global $wpdb;
  66. $dimoi_table = $wpdb->prefix . 'dimoi';
  67. $wp_dimos_search = $wpdb->get_results("SELECT dimos_ID, dimos_Title FROM $dimoi_table WHERE nomos_ID=$parentCat ORDER BY dimos_ID ASC");
  68.  
  69.               //Creating drop down item.
  70.               foreach ($wp_dimos_search as $dimos) {
  71.                 $option .= '<option value="'.$dimos->dimos_ID.'">';
  72.                 $option .= $dimos->dimos_Title;
  73.                 $option .= '</option>';
  74.               }
  75.                //   Adding other... item
  76.               echo '<option value="0" selected="selected">Select...</option>'.$option;
  77.       die();
  78.  } // end if   
  79. }
  80.  
  81. add_action( 'wp_ajax_nopriv_monitor_children_dropdown', 'monitor_children_dropdown'); //keep for people who allow post before registration
  82. add_action( 'wp_ajax_monitor_children_dropdown', 'monitor_children_dropdown');
RAW Paste Data