Created
April 20, 2019 04:27
-
-
Save yeriepiscesa/c1d099556c8a7b163e00ee02042dffdc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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