Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does one reload the contents of an element without refreshing the page after sending a form (The cake way)?
    primarykey
    data
    text
    <p>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?</p> <p>Here is my element:</p> <pre><code>&lt;?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 '&lt;div class="comment"&gt;'.$comment['content']. ' - '.$this-&gt;Html-&gt;link($comment['User']['username'],array('controller'=&gt;'users','action'=&gt;'view',$comment['User']['id'])) .'&lt;/div&gt;'; } echo $this-&gt;Form-&gt;create(null, array('url' =&gt; '/comments/add','id'=&gt;'qCommentForm')); echo $this-&gt;Html-&gt;link('Leave comment','javascript:toggleDisplay("comment'.$name.'")'); echo '&lt;br&gt;&lt;div id="comment'.$name.'" style="display:none;"&gt;'; echo $this-&gt;Form-&gt;input('Comment.parent_id', array('type'=&gt;'hidden','value'=&gt;$data[$type]['id'])); echo $this-&gt;Form-&gt;input('Comment.parent_type', array('type'=&gt;'hidden','value'=&gt;$type)); echo $this-&gt;Form-&gt;textarea('Comment.content',array('div'=&gt;'false','class'=&gt;'small','label'=&gt;false)); echo $this-&gt;Form-&gt;submit(__('Leave comment'),array('div'=&gt;'false','class'=&gt;'small')); echo '&lt;/div&gt;'; echo $this-&gt;Form-&gt;end(); ?&gt; </code></pre> <h3>Update</h3> <p>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".</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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