Skip to content

Instantly share code, notes, and snippets.

@yeriepiscesa
Created April 20, 2019 04:27
Show Gist options
  • Save yeriepiscesa/c1d099556c8a7b163e00ee02042dffdc to your computer and use it in GitHub Desktop.
Save yeriepiscesa/c1d099556c8a7b163e00ee02042dffdc to your computer and use it in GitHub Desktop.
<?php
function wpcf7_get_administrative() {
global $wpdb;
check_ajax_referer( 'ajax-get-administrative', 'security' );
if( isset( $_POST['province'] ) ) {
$province_id = $_POST['province'];
$sql = $wpdb->prepare( "select * from {$wpdb->prefix}regencies where province_id=%s order by id asc", $province_id );
}
if( isset( $_POST['regency'] ) ) {
$regency_id = $_POST['regency'];
$sql = $wpdb->prepare( "select * from {$wpdb->prefix}districts where regency_id=%s order by id asc", $regency_id );
}
if( isset( $_POST['district'] ) ) {
$district_id = $_POST['district'];
$sql = $wpdb->prepare( "select * from {$wpdb->prefix}villages where district_id=%s order by id asc", $district_id );
}
$rows = $wpdb->get_results( $sql, OBJECT );
$data = array();
foreach( $rows as $row ) {
$data[ $row->id ] = $row->name;
}
echo json_encode( $data );
wp_die();
}
add_action( 'wp_ajax_get_administrative', 'wpcf7_get_administrative' );
add_action( 'wp_ajax_nopriv_get_administrative', 'wpcf7_get_administrative' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment