Last active
August 29, 2015 14:23
-
-
Save harisrozak/91a7178b43d346a8c7b5 to your computer and use it in GitHub Desktop.
WordPress :: ajax
This file contains hidden or 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 | |
add_action('wp_ajax_ajax_coba', 'ajax_coba' ); | |
function ajax_coba() | |
{ | |
$id = $_POST['id']; | |
// json return | |
$return = array( | |
'success' => true, | |
'array' => array( | |
'0' => array( | |
'id' => 1, | |
'title' => 'satu', | |
'description' => 'ini adalah satu' | |
), | |
'1' => array( | |
'id' => 2, | |
'title' => 'dua', | |
'description' => 'ini adalah dua' | |
), | |
'2' => array( | |
'id' => 3, | |
'title' => 'tiga', | |
'description' => 'ini adalah tiga' | |
) | |
) | |
); | |
echo json_encode($return); | |
die(); | |
} |
This file contains hidden or 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
jQuery(document).ready(function($) { | |
$("[coba]").click(function(){ | |
coba(); | |
}); | |
function coba() | |
{ | |
var data = { | |
action: 'ajax_coba', | |
id: 'coba id' | |
} | |
$.post(ajaxurl, data, function(response){ | |
var response = $.parseJSON(response); | |
if(response.success) | |
{ | |
for (var i = 0; i < response.array.length; i++) | |
{ | |
console.log(response.array[i]); | |
}; | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment