PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Dynamic ajax category child dropdown

  1. 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');
    Posted 12 years ago on Thursday May 24, 2012 | Permalink
  2. I resolve this issue
    If someone need dynamic category + child dropdown e.g. states + regions
    here is the code http://pastebin.com/wrXpvHgD
    (gform_pre_render_2) my form ID 2
    (field id 28) my empty single line text converted to dropdown
    (field id 31) my empty drop down
    allow field to be populated dynamically

    Cheers!

    Posted 12 years ago on Saturday May 26, 2012 | Permalink
  3. muops.dm
    Member

    Hi Stav,
    First of all thanks for your code. Putting table names and re-writing the above code:

    CREATE TABLE wp_cms_5_geo_COUNTRY (
       country_id INT NOT NULL AUTO_INCREMENT,
       country_name VARCHAR(200) NOT NULL,
       PRIMARY KEY ( country_id )
    );
    INSERT INTO wp_cms_5_geo_COUNTRY VALUES (1, 'India'),(2, 'America'),(3, 'Africa');
    
    CREATE TABLE wp_cms_5_geo_STATES (
       state_id INT NOT NULL AUTO_INCREMENT,
       state_name VARCHAR(200) NOT NULL,
       country_id INT,
       PRIMARY KEY ( state_id )
    );
    INSERT INTO wp_cms_5_geo_STATES VALUES (1, 'Rajasthan', 1),(2, 'Delhi', 1),(3, 'Punjab', 1),(4, 'Washington DC', 2),(5, 'New York', 2),(6, 'Cape Town', 3);
    <?php
    //Dynamic category dropdown
    add_filter("gform_pre_render_1", "monitor_single_dropdown");
    function monitor_single_dropdown($form){
    
    	global $wpdb;
    	$country_table = $wpdb->prefix . 'geo_COUNTRY';
    	$wp_nomos_search = $wpdb->get_results("SELECT country_ID, country_Name FROM $country_table ORDER BY country_ID ASC");
    
    	//Creating drop down item array.
    	$items = array();
    
    	//Adding nomos to the items array
    	foreach ( $wp_nomos_search as $nomos ){
    	$is_selected = $nomos->country_Name == "Rendered Title" ? true : false;
    	$items[] = array("value" => $nomos->country_ID, "text" => $nomos->country_Name, "isSelected"=> $is_selected);
    	}
    
        //Adding other... item
    	$items[] = array("text" => "Other...", "value" => "0");
    
    	 //Adding items to field id 4
    	    foreach($form["fields"] as &$field)
    
    	        if($field["id"] == 4){
    	            $field["type"] = "select";
    	            $field["choices"] = $items;
    }
    
    ?>
    <script type="text/javascript">
    
    jQuery(document).ready(function(){
    
    	jQuery('#input_1_4').change(function(){
    			var mainCat=jQuery('#input_1_4').val();
    
    			// call ajax
    			 jQuery("#input_1_5").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_1_5").append(results);
    								}
    						   });
    				  }
    			);
    });
    
    </script>
    
    <?php
    return $form;
    }
    
    //ajax callback function
    function monitor_children_dropdown() {
    if(isset($_POST['main_catid'])) {
    
    $parentCat=$_POST['main_catid'];
    global $wpdb;
    $states_table = $wpdb->prefix . 'geo_STATES';
    $wp_dimos_search = $wpdb->get_results("SELECT state_ID, state_Name FROM $states_table WHERE country_ID=$parentCat ORDER BY state_ID ASC");
    
                  //Creating drop down item.
    			  foreach ($wp_dimos_search as $dimos) {
    				$option .= '<option value="'.$dimos->state_ID.'">';
    				$option .= $dimos->state_Name;
    				$option .= '</option>';
    			  }
                   //	Adding other... item
    			  echo '<option value="0" selected="selected">Select...</option>'.$option;
    	  die();
     } // end if
    }
    
    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');
    ?>
    Posted 12 years ago on Monday July 2, 2012 | Permalink
  4. muops.dm
    Member

    Above code is almost similar to stav's except the table names and their column names. Also, I put my form ID (1) and Empty Single Line Text (4) and Empty DropDown Field (5).

    Allowed field(5) to be populated dynamically.

    Again awesome work around while Gravity Forms is not able to handle it on UI. Wish they implement it very soon to make it more handy.


    Any ideas/thoughts how this can be applied on 3 dependent dropdowns???

    Cheers!!
    Deepak

    Posted 12 years ago on Monday July 2, 2012 | Permalink
  5. Would you post link of where this is being used? I'd like to see it in action to determine if it fits what I need (displaying one post category only in dropdown). thanks.

    Posted 12 years ago on Friday August 10, 2012 | Permalink
  6. Hi,
    Thanks for your code, it really works! I saw on your page 3 different dropdowns working together. I'd like to see the code. I'm working on a project and need to do something like:
    The user fills the ZipCode field and I find on a database and fill the fields State, city and Street. Can you help me with this?

    Posted 12 years ago on Wednesday September 12, 2012 | Permalink
  7. Hi Leonardo,

    Did you ever get a third level working? Please let me know.

    Thanks,
    John

    Posted 11 years ago on Thursday April 25, 2013 | Permalink
  8. downsy42
    Member

    On which php page are you placing this code??

    Posted 11 years ago on Friday June 28, 2013 | Permalink
  9. Richard Vav
    Administrator

    If you still require assistance with this please open a new support ticket or a priority support ticket if you are a developer license holder. Thank you.

    Posted 11 years ago on Saturday July 27, 2013 | Permalink

This topic has been resolved and has been closed to new replies.