问题的Ajax助手在蛋糕PHP [英] Problem with Ajax Helper in Cake PHP
问题描述
我想在我的博客的评论部分实现Ajax功能。我已经下载了原型1.6.0.3.js,并将其放在webroot的js文件夹中。
我在布局文件(default.ctp)中进行了以下更改
$ javascript-> link array('prototype'));
此外,以下代码已添加到控制器中
var $ helpers = array('Html','Form','Ajax','Javascript');
这是我在posts_controller.php文件中的代码
函数视图($ 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文件中的代码
< h2>评论< / 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'=>'comment'));?>
<?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>
我在comments_controller.php中添加了以下函数
function add(){
if(!empty($ this-> data)){
$ this-> Comment->创建();
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-& add_failure','ajax');
}
}
}
以下是add_success.ctp文件中的代码
<?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;?>
我不能添加评论,当我点击添加评论按钮没有什么发生。我手动添加评论在数据库和工作。但是我在尝试Ajax助手时遇到问题。
问题出在哪里,对不起这么长的问题[:(] 你是否安装了firebug,你可以测试一下是否正在发出ajax请求?
也许尝试做$ form-> submit('Add Comment');生成按钮,看看是否有区别,并实际执行ajax请求。
您有任何JavaScript错误吗?
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'));
Also, following code has been added to controllers
var $helpers = array('Html', 'Form', 'Ajax','Javascript');
This is my code in the posts_controller.php file
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'));
}
My code in view.ctp file
<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>
I have added the following function in the comments_controller.php
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');
}
}
}
And following is the code in the add_success.ctp file
<?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;?>
Now the problem is that I am not able to add comments. Nothing happens when I click on the add comments button. I have manually added comments in the databse and that works. But I am having problems when I am trying the Ajax Helper.
Where is the problem??And guys, sorry for such a long question.[:(]
Do you have firebug installed so you can test to see if it is making the ajax request?
Perhaps try doing $form->submit('Add Comment'); to generate the button and see if it makes a difference and actually performs the ajax request.
Do you get any javascript errors?
这篇关于问题的Ajax助手在蛋糕PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!