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

  • Home
  • SEARCH
  • 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 8014443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:56:12+00:00 2026-06-04T19:56:12+00:00

I have an element (a comments list and form) that is used in many

  • 0

I have an element (a comments list and form) that is used in many places in my application. It works all fine and dandy, except it requires refreshing the entire page. This can be problematic, especially when it resets the game to which your comment belongs, causing all progress to be tragically lost. I have very limited experience with AJAX, so what is the most effective, simplest way to reload the element with the added comment?

Here is my element:

<?php
/*
set variables:
$data : data of the parent
$type : the type of the parent
$name : (optional)unique name to avoid naming conflicts
*/
if(!isset($name)) {
$name = 0;
}
foreach($data['Comment'] as $comment){
    echo '<div class="comment">'.$comment['content'].
        ' - '.$this->Html->link($comment['User']['username'],array('controller'=>'users','action'=>'view',$comment['User']['id']))
        .'</div>';
}
echo $this->Form->create(null, array('url' => '/comments/add','id'=>'qCommentForm'));
echo $this->Html->link('Leave comment','javascript:toggleDisplay("comment'.$name.'")');
echo '<br><div id="comment'.$name.'" style="display:none;">';
echo $this->Form->input('Comment.parent_id', array('type'=>'hidden','value'=>$data[$type]['id']));
echo $this->Form->input('Comment.parent_type', array('type'=>'hidden','value'=>$type));
echo $this->Form->textarea('Comment.content',array('div'=>'false','class'=>'small','label'=>false));
echo $this->Form->submit(__('Leave comment'),array('div'=>'false','class'=>'small'));
echo '</div>';
echo $this->Form->end();
?>

Update

Okay, so I understand a lot more about ajax thanks to your posts, but I still do not understand how to do this the “cake way”.

  • 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-06-04T19:56:14+00:00Added an answer on June 4, 2026 at 7:56 pm

    With HTML like this:

    <div id="comments">
        <!-- list of comments here -->
    </div>
    
    <form method="post" action="/comments/add" id="qCommentForm">
        <textarea name="Comment.content"></textarea>
        <input type="submit" value="Leave comment">
    </form>
    

    You can use JavaScript (and jQuery in this case) to intercept the submit event and send the comment data with Ajax (assuming the PHP form handler returns an HTML fragment for the new comment):

    // run on document ready
    $(function () {
        // find the comment form and add a submit event handler
        $('#qCommentForm').submit(function (e) {
            var form = $(this);
    
            // stop the browser from submitting the form
            e.preventDefault();
    
            // you can show a "Submitting..." message / loading "spinner" graphic, etc. here
    
            // do an Ajax POST
            $.post(form.prop('action'), form.serialize(), function (data) {
                // append the HTML fragment returned by the PHP form handler to the comments element
                $('#comments').append(data);
            });
        });
    });
    

    If the PHP form handler returns the whole list of comments (as HTML) instead of just the new one, you can use .html() instead of .append():

    $('#comments').html(data);
    

    You can find the jQuery documentation at http://docs.jquery.com/.


    Update: I’m not a CakePHP expert, but the “cake way” AFAICT:

    1. Set up JsHelper:

      1. Download your preferred JavaScript library

      2. Include the library in your view/layout, e.g.

        echo $this->Html->script('jquery');
        
      3. Write the JsHelper buffer in your view/layout, e.g.

        echo $this->Js->writeBuffer();
        
      4. Include JsHelper in your controller, e.g.

        public $helpers = array('Js' => array('Jquery'));
        
    2. Use JsHelper::submit() instead of FormHelper::submit() to generate a submit button that will do Ajax form submission, e.g.

      echo $this->Js->submit('Leave comment', array('update' => '#comments'));
      
    3. In your controller, detect if the request is an Ajax request, and if so, render using the Ajax layout, e.g.

      if ($this->request->is('ajax')) {
          $this->render('comments', 'ajax');
      }
      

    Not sure if/how RequestHandlerComponent figures into this though.

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

Sidebar

Related Questions

I have a web app that has a list of user comments. I want
If have an element that maybe disabled on a page, and would like to
I have an element that can have multiple types (not my design). The element
I am creating a website that will have articles; each article will have comments.
I have these these two functions //Remove all even indexed elements from a list
I have a class that looks like this: typedef std::list<char*> PtrList; class Foo {
I have a script that compiles a list of items that can be very
I have a list similar to this: host_info = ['stackoverflow.com', '213.213.214.213', 'comments', 'desriprion', 'age']
i have a listview that use a simple adapter. Basically each element consist of
I am trying to remove comments from a list of elements that were obtained

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.