2011-11-03 230 views
1

我一直在閱讀關於使用Zend的Ajax和Jquery,但我似乎無法得出的想法。例如,我有一個簡單的帖子,有一些評論,我希望發佈評論到頁面而不需要刷新。Zend Framework,發表評論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; 
} 

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

和m y控制器:

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


} 
+1

你試過的javascript在哪裏? – vascowhite

+0

我已經編輯我的帖子與我的企圖。 – BalintD

回答

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

視圖操作不應該包含添加註釋的方法。相反,做一個addcoment行動。然後,表單將包含新聞的ID或任何附加評論。您通過ajax發送表單,並且由於$this->getRequest()->isXmlHttpRequest(),您可以檢查該帖子是否來自ajax或手動添加javascript。

取決於你返回一個成功和錯誤的json回調。您不僅會回饋'成功',而且您還可以回饋在json中添加的評論。

jQuery處理其餘的部分。適當地格式化評論,將其添加到您的DOM和中提琴。添加了評論。

稍加引導,這應該是足夠的,如果u想要的任何上述提到的具體信息,打了我一個評論,生病進一步編輯我的回答則;)

+0

好的,所以我必須在我的控制器中做一個新的動作來處理我的表單,我有些時候會得到它。現在,我不明白的部分,我將如何在View操作視圖文件中呈現此操作? 另外要清楚的是,我甚至會如何將我的評論表格的內容發送回此類操作? – BalintD

+0

好吧,@ubercooluk告訴你如何通過ajax發佈內容。您將ajax-call綁定到您的提交點擊並將表單的數據發送到某個網址,如「project.tld/addcomment」。 addcomment然後只是將註釋添加到數據庫。此外,如果它是一個ajax調用(請參閱答案),它會返回一些json(google zend return json) - 評論的呈現主要是從新聞的「視圖」動作完成的,就像現在一樣。要在新評論之後刷新,只需「手動」添加它,或者創建另一個操作,該操作僅返回新聞的評論,並用回調代替當前:) – Sam

+0

好吧,我嘗試了@ubercooluk的代碼,並且還做了一個新操作以增加評論,但我們甚至沒有接近。請告訴我在哪裏需要改進我的代碼。非常感謝你的幫助!哦,順便我更新了我的代碼。 – BalintD