Zend框架,使用ajax和jquery发布评论

Zend框架,使用ajax和jquery发布评论,ajax,zend-framework,Ajax,Zend Framework,我一直在阅读Zend使用Ajax和Jquery的相关知识,但我似乎无法理解这个想法。例如,我有一篇带有一些评论的简单帖子,我希望在不需要刷新的情况下向页面发布评论 以下是我所拥有的: //Controller public function viewAction() { // action body $postid = $this->_getParam('id', 0); $post = new Application_Model_DbTable_Videos

我一直在阅读Zend使用Ajax和Jquery的相关知识,但我似乎无法理解这个想法。例如,我有一篇带有一些评论的简单帖子,我希望在不需要刷新的情况下向页面发布评论

以下是我所拥有的:

 //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;
}


查看操作不应包含添加注释的方法。而是做一个
addcoment
操作。然后,表单包含新闻或其他内容的ID以及评论。您通过ajax发送表单,由于
$this->getRequest()->isXmlHttpRequest()
您可以检查帖子是否来自ajax或不带javascript的手动添加

根据返回成功和错误的json回调。您不仅可以返回“成功”,还可以返回json中添加的注释

jQuery处理其余部分。适当格式化注释,将其添加到DOM和viola中。评论补充道


作为一个小小的指导,这应该足够了,如果你想了解上述任何一项的具体信息,请给我留言,然后我会进一步编辑我的答案;)

查看操作不应包含添加注释的方法。而是做一个
addcoment
操作。然后,表单包含新闻或其他内容的ID以及评论。您通过ajax发送表单,由于
$this->getRequest()->isXmlHttpRequest()
您可以检查帖子是否来自ajax或不带javascript的手动添加

根据返回成功和错误的json回调。您不仅可以返回“成功”,还可以返回json中添加的注释

jQuery处理其余部分。适当格式化注释,将其添加到DOM和viola中。评论补充道

作为一个小小的指导,这应该足够了,如果你想了解上述任何一项的具体信息,请给我留言,然后我会进一步编辑我的答案;)

像这样做

    $(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');
             }
          });
    });
    });
这样做,

    $(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');
             }
          });
    });
    });

你尝试过的javascript在哪里?我尝试过编辑我的帖子。你尝试过的javascript在哪里?我尝试过编辑我的帖子。好的,所以我必须在我的控制器中执行一个新操作,该操作将处理我的表单,这是我得到的。现在我不明白的部分是,如何在我的视图操作文件中呈现这个操作?同样要明确的是,我甚至应该如何将我的评论表单的内容发送回这样的行动?嗯,@ubercooluk确实告诉过你如何通过ajax发布一些东西。将ajax调用绑定到submit click,并将表单数据发送到某个url,如“project.tld/addcomment”。addcomment然后只将注释添加到数据库中。此外,如果是ajax调用(请参见答案),它将返回一些json(google zend return json)——评论的呈现主要是通过新闻的“查看”操作完成的,就像您现在所做的那样。要在新评论后刷新,只需“手动”添加它,或执行另一个操作,仅返回新闻的评论并将当前内容替换为回调:)好的,因此我尝试了@ubercooluk中的代码,并执行了一个新操作以添加评论,但还没有完成。请告诉我哪里需要改进我的代码。非常感谢您迄今为止的帮助!!!哦,顺便说一下,我更新了上面的代码。好的,我必须在我的控制器中进行一个新的操作,它将处理我的表单,这是我得到的。现在我不明白的部分是,如何在我的视图操作文件中呈现这个操作?同样要明确的是,我甚至应该如何将我的评论表单的内容发送回这样的行动?嗯,@ubercooluk确实告诉过你如何通过ajax发布一些东西。将ajax调用绑定到submit click,并将表单数据发送到某个url,如“project.tld/addcomment”。addcomment然后只将注释添加到数据库中。此外,如果是ajax调用(请参见答案),它将返回一些json(google zend return json)——评论的呈现主要是通过新闻的“查看”操作完成的,就像您现在所做的那样。要在新评论后刷新,只需“手动”添加它,或执行另一个操作,仅返回新闻的评论并将当前内容替换为回调:)好的,因此我尝试了@ubercooluk中的代码,并执行了一个新操作以添加评论,但还没有完成。请告诉我哪里需要改进我的代码。非常感谢您迄今为止的帮助!!!哦,顺便说一下,我更新了上面的代码。
 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();
        }
    }


}
    $(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');
             }
          });
    });
    });