Making some assumptions here…
In your HTML where you loop your children have a button something like:
<% loop $Children %>
<button type="button" class="blog-like-button" data-id="$ID"></button>
<% end_loop %>
In jQuery:
$('.blog-like-button').click(function()
{
$.post('/blog/like', {'id': $(this).attr('data-id')}, function (resp)
{
// Handle resp(onse)
}, 'json');
});
That’ll give you a unique button for each post with a click handler to POST the PostPage ID to a controller.
Assuming you already have a controller for /blog, add “like” to the allowed actions:
private static $allowed_actions = ['like'];
Write a like function to increment and save the record:
public function like(HTTPRequest $request)
{
# If it's not ajax we don't want to know
if ($request->isAjax())
{
# Set a pessimistic default
$json = ['success' => false];
# Get the id from the data attribute on the button we sent
$id = $request->postVar('id');
# Use it to the find the post page. Change PostPage to w/e yours is
$post = PostPage::get()->byID($id);
# If we found a page
if ( ! empty($post))
{
# Increment the like's variable
$post->Likes = $post->Likes + 1;
$post->write();
# Success!
$json = ['success' => true];
}
return json_encode($json);
}
}
Lastly, add the “like” field to your page in it’s $db definition:
private static $db = [
'Likes' => 'Int'
];