我一直在阅读使用Ajnd和Jquery和Zend,但我似乎无法理解这个想法。例如,我有一个带有一些注释的简单帖子,我希望在不需要刷新的情况下向页面发表评论。
这就是我所拥有的:
//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']; ?><br>
<?php echo $this->post['Description']; ?><br><br>
<?php if( count($this->comments) ) : ?>
<?php foreach( $this->comments as $comment ) : ?>
<a href="<?php echo $comment['Webpage']; ?>" ><?php echo $this->escape( $comment['Name'] ); ?></a> on <span>
<?php echo $this->escape( date( 'd-m-Y', strtotime($comment['Postedon']) ) ); ?></span><br><br>
<?php echo $this->escape( $comment['Description'] ); ?><br>
<?php endforeach; ?>
<?php else : ?>
<div>No comments</div><br>
<?php endif; ?>
<br>
<?php echo $this->commentsForm; ?>
请指导我某个方向,因为我花了很多时间没有运气:(
尝试更新:
//View file
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
$(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');
}
});
});
});
</script>
<?php echo $this->post['Title']; ?><br>
<?php echo $this->post['Description']; ?><br><br>
<?php if( count($this->comments) ) : ?>
<?php foreach( $this->comments as $comment ) : ?>
<a href="<?php echo $comment['Webpage']; ?>" ><?php echo $this->escape( $comment['Name'] ); ?></a> on <span>
<?php echo $this->escape( date( 'd-m-Y', strtotime($comment['Postedon']) ) ); ?></span><br><br>
<?php echo $this->escape( $comment['Description'] ); ?><br>
<?php endforeach; ?>
<?php else : ?>
<div>No comments</div><br>
<?php endif; ?>
<br>
<?php echo $this->commentsForm; ?>
我的控制员:
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 :(得分:1)
视图操作不应包含添加注释的方法。而是做出addcoment
动作。然后,表格包含新闻或任何内容以及评论的ID。您通过ajax发送了表单,由于$this->getRequest()->isXmlHttpRequest()
,您可以检查帖子是来自ajax还是手动添加witihout javascript。
根据您返回成功和错误的json回调。你不仅要回馈'成功',而且你也可以回复json中添加的评论。
jQuery处理剩下的事情。正确格式化评论,将其添加到您的DOM和中提琴。评论补充说。
作为一点指导,这应该足够了,如果你想要上述任何一个的具体信息,请给我发表评论,然后进一步编辑我的答案;)
答案 1 :(得分:0)
这样做,
$(document).ready(function(){
$('#your_comment_save_button_id').click(function(){
var comments = $('#commentbox_id').val();
$.ajax({
url : 'Your comment save page url',
type : 'POST',
data : {'commments_post':comments },
success:function(msg){ //msg a variable echoed from the save page
if(msg=='Ok'){
alert('You have saved the comment with out refresh');
}else{
alert('cant save');
}
},
error:function()
{
alert('Error');
}
});
});
});