Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // function at top of admin page backend
- add_action('wp_ajax_services_get_regions', 'services_get_regions');
- function services_get_regions(){
- $group = $_GET['service_option_group'];
- $regions_table = 'v_region';
- // Array to hold data
- $bigArray = array();
- // Variable to determine the select clause
- $query = "";
- $query = "SELECT * FROM $regions_table WHERE `group` = $group ";
- $regions = $wpdb->get_results($query);
- foreach($regions as $region) {
- array_push($bigArray, $region);
- }
- header( "Content-Type: application/json" );
- echo json_encode($bigArray);
- //Don't forget to always exit in the ajax function.
- exit();
- }
- //Request on page that holds the page options for that tab in an include etc... <?php require_once('add_service.php'); ?>
- // watch for the region type selector to change, when it does update the region list
- jQuery('#new_service #service_option_group').on('change', function() {
- // Ajax query to fetch the results
- jQuery.ajax({
- type: 'get',
- url: ajaxurl,
- data: {
- action: 'services_get_regions',
- data: jQuery('#service_option_group').serialize()
- },
- dataType: 'json',
- success: function(result) {
- jQuery('#new_service #service_option_region').empty();
- // need to add the default option back in
- var option = document.createElement('option');
- var select = document.getElementById('service_option_region');
- option.text = 'Select a Region'
- option.value = -1;
- select.appendChild(option);
- // Append on the events
- for (var i = 0; i < result.length; i++) {
- // create and append each element
- var option = document.createElement('option');
- // result type will differ if its city or event
- option.text = result[i].title;
- option.value = result[i].id;
- var select = document.getElementById('service_option_region');
- select.appendChild(option);
- }
- },
- error: function(request, status, error) {
- alert(request.responseText);
- }
- })
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement