Skip to content

Instantly share code, notes, and snippets.

@harisrozak
Last active August 29, 2015 14:23
Show Gist options
  • Save harisrozak/91a7178b43d346a8c7b5 to your computer and use it in GitHub Desktop.
Save harisrozak/91a7178b43d346a8c7b5 to your computer and use it in GitHub Desktop.
WordPress :: ajax
<?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();
}
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