I'm using phalcon framework and In my blog i want to append comments with ajax but the problem is in my controller query. i don't understand how to return query data to view with ajax. my current query script return entire hole html page but i need only commented data. someone please help
[Controller]
public function detailsAction($id)
{
$blog = Blogs::findFirstByid($id);
#Comments Retrieve
$coment = Comments::find();
}
public function bCommentAction()
{
if($this->session->has('uname'))
{
if($this->request->isPost() == true || $this->request->isAjax() == true)
{
$comments = new Comments();
$comments->cauthor = trim($this->session->get('uname'));
$comments->bcoment = trim($this->request->getPost('bComent'));
$comments->entry_id = trim($this->request->getPost('postId'));
if(!$comments->create() == true)
{
$this->flashSession->success("SUCCESS:: Comments inserted");
return $this->response->redirect($this->request->getHTTPReferer());
}
else
{
return $this->response->setJsonContent(['bcoment' => $comments->bcoment,
'cauthor' => $comments->cauthor, 'entry_id' => $comments->entry_id]);
}
}
else{echo('Request is Not ajax!');}
}
else
{
echo('<script>alert("You are not loggedin!");</script>');
}
$this->view->disabled();
}
[Jquery]
$('#blogComment').submit(function(e){
e.preventDefault();
var blogComent = $("input[name='bComent']").val();
var postsId = $("input[name='idPost']").val();
$.ajax({
url:'blog/bComment/',
type: 'POST',
cache: false,
data: {'postId':postsId,'bComent':blogComent},
success: function(data){
$('#datax').append('<div class="bcom">'+json.stringify(data)+'</div>').fadeIn(500);
}
});
return false;
});
[View]
<div id="datax">
{% for coment in comented %}
{% if coment.entry_id === detail.id %}
<div class="bcom">
{% for user in logged %}
{% if coment.cauthor === user.uname %}
{{ image('data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=',"alt": "Some Photo","data-src":"uploads/users/"~user.image,"title":""~user.uname,"class":"comentedId") }}
{% endif %}
{% endfor %}
<b>{{coment.cauthor}}</b><br/>
{{coment.bcoment}}
</div>
{% endif %}
{% endfor %}</div>
Output looks like image below: