我一直在閱讀關於使用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();
}
}
}
你試過的javascript在哪裏? – vascowhite
我已經編輯我的帖子與我的企圖。 – BalintD