zendframework jquery ajax,Zend Framework, posting comment with ajax and jquery

I have been reading up on using Ajax and Jquery with Zend but I can not seem to get the idea down. For example I have a simple post with some comments, I wish to post a comment to the page without the need to refresh.

Here is what I have:

//Controller

public function viewAction()

{

// action body

$postid = $this->_getParam('id', 0);

$post = new Application_Model_DbTable_Videos();

$this->view->post = $post->getVideos($postid);

$commentsObj = new Application_Model_DbTable_Comments();

$request = $this->getRequest();

$commentsForm = new Application_Form_Comments();

/*

* Check the comment form has been posted

*/

if ($this->getRequest()->isPost()) {

if ($commentsForm->isValid($request->getPost())) {

$model = new Application_Model_DbTable_Comments();

$model->saveComments($commentsForm->getValues());

$commentsForm->reset();

}

}

$data = array( 'id'=> $postid );

$commentsForm->populate( $data );

$this->view->commentsForm = $commentsForm;

$comments = $commentsObj->getComments($postid);

$this->view->comments = $comments;

$this->view->edit = '/videos/edit/id/'.$postid;

}

//View

<?php echo $this->post['Title']; ?>

<?php echo $this->post['Description']; ?>

<?php if( count($this->comments) ) : ?>

<?php foreach( $this->comments as $comment ) : ?>

<?php echo $this->escape( $comment['Name'] ); ?> on

<?php echo $this->escape( date( 'd-m-Y', strtotime($comment['Postedon']) ) ); ?>

<?php echo $this->escape( $comment['Description'] ); ?>

No comments

<?php echo $this->commentsForm; ?>

Please guide me in some sort of direction because I have spent many hours with no luck :(

Update with my attempt:

//View file

$(document).ready(function(){

$('submitbutton').click(function(){

var comments = $('comment').val();

$.ajax({

url : 'localhost/kiwi/public/videos/addcomment',

type : 'POST',

data : {'commments_post':comments },

success:function(msg){

if(msg=='Ok'){

alert('You have saved the comment with out refresh');

}else{

alert('cant save');

}

},

error:function()

{

alert('Error');

}

});

});

});

<?php echo $this->post['Title']; ?>

<?php echo $this->post['Description']; ?>

<?php if( count($this->comments) ) : ?>

<?php foreach( $this->comments as $comment ) : ?>

<?php echo $this->escape( $comment['Name'] ); ?> on

<?php echo $this->escape( date( 'd-m-Y', strtotime($comment['Postedon']) ) ); ?>

<?php echo $this->escape( $comment['Description'] ); ?>

No comments

<?php echo $this->commentsForm; ?>

And my controller:

public function viewAction()

{

// action body

$postid = $this->_getParam('id', 0);

$post = new Application_Model_DbTable_Videos();

$this->view->post = $post->getVideos($postid);

$commentsObj = new Application_Model_DbTable_Comments();

$commentsForm = new Application_Form_Comments();

$data = array( 'id'=> $postid );

$commentsForm->populate( $data );

$this->view->commentsForm = $commentsForm;

$comments = $commentsObj->getComments($postid);

$this->view->comments = $comments;

}

public function addcommentAction()

{

$request = $this->getRequest();

$commentsForm = new Application_Form_Comments();

$commentsObj = new Application_Model_DbTable_Comments();

if ($this->getRequest()->isPost()) {

if ($commentsForm->isValid($request->getPost())) {

$model = new Application_Model_DbTable_Comments();

$model->saveComments($commentsForm->getValues());

$commentsForm->reset();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值