I am trying to implement the Ajax feature in the comments section of my blog. I have downloaded prototype-1.6.0.3.js and have placed it in the js folder inside webroot. I have made the following changes in the layout file(default.ctp)
$javascript->link(array('prototype'));
Aynı zamanda, aşağıdaki kod, kontrol eklendi
var $helpers = array('Html', 'Form', 'Ajax','Javascript');
Bu posts_controller.php dosyasında benim kodu
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid Post.', true));
$this->redirect(array('action'=>'index'));
}
$post = $this->Post->read(null,$id);
$comments = $this->Post->Comment->find('all',
array('conditions'=>array('Post.id'=>$id)));
$this->set(compact('post','comments'));
}
View.ctp dosyasında Benim kod
<h2>Comments</h2>
<div id="comments">
<?php foreach($comments as $comment): ?>
<div class="comment">
<p><b><?php echo $comment['Comment']['name']; ?></b></p>
<p><?php echo $comment['Comment']['content']; ?></p>
</div>
<?php endforeach; ?>
<?php echo $ajax->form('/comments/add','post',array('update'=>'comments'));?>
<?php echo $form->input('Comment.name');?>
<?php echo $form->input('Comment.content');?>
<?php echo $form->input('Comment.post_id',array('type'=>'hidden','value'=>$post['Post']['id']));?>
<?php echo $form->end('Add Comment');?>
</div>
Ben comments_controller.php aşağıdaki fonksiyonu da ekledik
function add() {
if (!empty($this->data)) {
$this->Comment->create();
if ($this->Comment->save($this->data)) {
$comments = $this->Comment->find('all',array('conditions'=>array('post_id'=>$this->data['Comment']['post_id']),'recursive'=>-1);
$this->set(compact('comments'));
$this->render('add_success','ajax');
} else {
$this->render('add_failure','ajax');
}
}
}
Ve sonra add_success.ctp dosyasındaki kodudur
<?php foreach($comments as $comment): ?>
<div class="comment">
<p><b><?php echo $comment['Comment']['name'];?></b></p>
<p><?php echo $comment['Comment']['content'];?></p>
</div>
<?php endforeach;?>
Şimdi sorun ben yorum eklemek mümkün değilim olmasıdır. Ben add comments butonuna tıkladığımda hiçbir şey olmuyor. Ben elle databse yorum eklemiş ve çalışıyor. Ben Ajax Yardımcısı çalışıyorum ama ne zaman sorunlar yaşıyorum.
? Nerede sorun Ve adamlar, böyle uzun bir soru için üzgünüm?. [: (]