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