Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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 nomos to the items array
- foreach ( $wp_nomos_search as $nomos ){
- $is_selected = $nomos->nomos_Title == "Θεσσαλονίκης" ? true : false;
- $items[] = array("value" => $nomos->nomos_ID, "text" => $nomos->nomos_Title, "isSelected"=> $is_selected);
- }
- //Adding other... item
- $items[] = array("text" => "Other...", "value" => "0");
- //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_31").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_31").append(results);
- }
- });
- }
- );
- });
- </script>
- <?php
- return $form;
- }
- //ajax callback function
- function monitor_children_dropdown() {
- if(isset($_POST['main_catid'])) {
- $parentCat=$_POST['main_catid'];
- global $wpdb;
- $dimoi_table = $wpdb->prefix . 'dimoi';
- $wp_dimos_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.
- foreach ($wp_dimos_search as $dimos) {
- $option .= '<option value="'.$dimos->dimos_ID.'">';
- $option .= $dimos->dimos_Title;
- $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');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement