Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with Ajax Helper in Cake PHP
    text
    copied!<p>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)</p> <pre><code>$javascript-&gt;link(array('prototype')); </code></pre> <p>Also, following code has been added to controllers</p> <pre><code>var $helpers = array('Html', 'Form', 'Ajax','Javascript'); </code></pre> <p>This is my code in the posts_controller.php file</p> <pre><code>function view($id = null) { if (!$id) { $this-&gt;Session-&gt;setFlash(__('Invalid Post.', true)); $this-&gt;redirect(array('action'=&gt;'index')); } $post = $this-&gt;Post-&gt;read(null,$id); $comments = $this-&gt;Post-&gt;Comment-&gt;find('all', array('conditions'=&gt;array('Post.id'=&gt;$id))); $this-&gt;set(compact('post','comments')); } </code></pre> <p>My code in view.ctp file</p> <pre><code>&lt;h2&gt;Comments&lt;/h2&gt; &lt;div id="comments"&gt; &lt;?php foreach($comments as $comment): ?&gt; &lt;div class="comment"&gt; &lt;p&gt;&lt;b&gt;&lt;?php echo $comment['Comment']['name']; ?&gt;&lt;/b&gt;&lt;/p&gt; &lt;p&gt;&lt;?php echo $comment['Comment']['content']; ?&gt;&lt;/p&gt; &lt;/div&gt; &lt;?php endforeach; ?&gt; &lt;?php echo $ajax-&gt;form('/comments/add','post',array('update'=&gt;'comments'));?&gt; &lt;?php echo $form-&gt;input('Comment.name');?&gt; &lt;?php echo $form-&gt;input('Comment.content');?&gt; &lt;?php echo $form-&gt;input('Comment.post_id',array('type'=&gt;'hidden','value'=&gt;$post['Post']['id']));?&gt; &lt;?php echo $form-&gt;end('Add Comment');?&gt; &lt;/div&gt; </code></pre> <p>I have added the following function in the comments_controller.php</p> <pre><code>function add() { if (!empty($this-&gt;data)) { $this-&gt;Comment-&gt;create(); if ($this-&gt;Comment-&gt;save($this-&gt;data)) { $comments = $this-&gt;Comment-&gt;find('all',array('conditions'=&gt;array('post_id'=&gt;$this-&gt;data['Comment']['post_id']),'recursive'=&gt;-1); $this-&gt;set(compact('comments')); $this-&gt;render('add_success','ajax'); } else { $this-&gt;render('add_failure','ajax'); } } } </code></pre> <p>And following is the code in the add_success.ctp file</p> <pre><code>&lt;?php foreach($comments as $comment): ?&gt; &lt;div class="comment"&gt; &lt;p&gt;&lt;b&gt;&lt;?php echo $comment['Comment']['name'];?&gt;&lt;/b&gt;&lt;/p&gt; &lt;p&gt;&lt;?php echo $comment['Comment']['content'];?&gt;&lt;/p&gt; &lt;/div&gt; &lt;?php endforeach;?&gt; </code></pre> <p>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.</p> <p>Where is the problem??And guys, sorry for such a long question.[:(]</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload