Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 326901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:21:08+00:00 2026-05-12T09:21:08+00:00

I am trying to implement the Ajax feature in the comments section of my

  • 0

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.[:(]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:21:08+00:00Added an answer on May 12, 2026 at 9:21 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement the HTML5 history feature to perform AJAX loading of page
I am trying to implement a feature and I have no idea how to
I am trying to implement AJax on Umbraco 5(jupiter), i have tried to create
I'm using magento as my CMS, trying to implement ajax for pagination, I have
I'm trying to implement an ajax page loading icon, but of course I can't
I am trying to implement a simple request to Wikipedia's API using AJAX (XMLHttpRequest).
I'm trying to implement authentication using jquery to make an ajax request to a
I'm trying to implement a form that utilizes jquery's post feature to dynamically update
I am trying to implement AJAX in my Google App Engine application, and so
I'm trying to implement a quite easy ajax call but since its my first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.