I am using Phalcon framework, I have a controller 'Post' with action 'like':
views/layouts/post.volt
{% for post in posts %}
<h1> {{ post.content }} <⁄h1>
<h2> {{ post.like }} <⁄h2>
<a href="#" onclick="like();" data-id="{{ post.id }}" >Like<⁄a>
{% endfor %}
<!-- -->
<script>
var like = function() { var id = /* ... */; // get id of post which raise 'like' event $.ajax({ dataType: 'json', url: 'post/like/' + id }).done(function() { /* Do something meaningful */ });
<⁄script>
controllers/PostController.php
/* ... */
public function indexAction() {
$this->view->posts = /* ... */; // read from database to show the view
}
public function likeAction($id) {
// change 'like' of corresponding post with certain 'id' foreach($this->view->posts as post) { if($post->id == $id) $post->like += 1; /* ... */ } // write to database, and re-render entire post view $this->view->posts = /* ... */; // I just assign 'view' again and hope it works :( $result = array('msg' => 'success'); $response = new Response(); return $response->setJsonContent($result);
}
/* ... */
But after clicking the button, nothing has changed! Did I do something wrong? Can anyone help me... Thank in advance!
P/S: sorry for my bad English... ^^
'$this->view->posts' is set only in 'indexAction'. It is empty when when 'likeAction'.