lundi 2 février 2015

autocomplete automatically wordpress custom post dropdown attached to taxonomy term using Ajax and jQuery


Please, I want to create a wordpress-Ajax and jQuey driven form, that sends a bench of informations about a list of schools. That form will process data from custom post type" School" related to an hierarchical custom taxonomy "localisation" stored in the wordpress admin, this taxonomy has a parent term that is "region" and a child term "city". And the form fields are : a select dropdown list of regions that linked to their respective cities in another dropdown select. I have also four checkbox selectors to decide either show on an autocomplete field for selecting schools related to the choosen region term in the previous select (ie : if the checkbox is "student" then show on the autocomplete school field , and if the checkbox is a student parent , then hide the autocomplete school field. Fortunately I succeded to select cities per region using the trio Wordpress-Ajax and jQuery.But I have an issue to get the school autocomplete list attached to their respective region worked. There is the code snippets for regions and their cities liaison.


There is how the form have to be : Form description


There is my PHP code in function.php to populate the cities dropdown with cities related to their choosen region:


function implement_ajax() { if(isset($_POST['main_catid'])) { $categories= get_categories('child_of='.$_POST['main_catid'].'&hide_empty=0'.'&taxonomy=localisation'); foreach ($categories as $cat) { $option .= 'term_id.'">'; $option .= $cat->cat_name; $option .= ''; } echo 'Villes'.$option; die(); } // end if } add_action('wp_ajax_my_special_ajax_call', 'implement_ajax'); add_action('wp_ajax_nopriv_my_special_ajax_call', 'implement_ajax');


And the JS code to send Ajax request:


$(function() { $('#main_cat').change(function() { var $mainCat=$('#main_cat').val(); // call ajax $("#sub_cat").empty(); $.ajax ( { url:"/wp-admin/admin-ajax.php", type:'POST', data:'action=my_special_ajax_call&main_catid=' + $mainCat, success:function(results) { // alert(results); $("#sub_cat").removeAttr("disabled"); $("#sub_cat").append(results); } } ); }); });

Now please I want to know how to figure out to handle the checkbox choises and after that getting the schools in the autocomplete select field ( ie: Etablissement field )


PS: Région field is the region field , Ville referes to cities select field and Etablissement field is for the custum post type "Schools" autocomplete.





Aucun commentaire:

Enregistrer un commentaire