views:

709

answers:

5

Hello, I've created a blog site from "Beginning CakePHP from novice to professiona"-David Golding. I have the comment view listed below:

<div class="comments form">
<?php echo $form->create('Comment');?>
    <fieldset>
     <legend><?php __('Add Comment');?></legend>
    <?php
     echo $form->input('name');
     echo $form->input('content');
     echo $form->input('post_id');
    ?>
    </fieldset>
<?php echo $form->end('Submit');?>
</div>
<div class="actions">
    <ul>
     <li><?php echo $html->link(__('List Comments', true), array('action' => 'index'));?></li>
     <li><?php echo $html->link(__('List Posts', true), array('controller' => 'posts', 'action' => 'index')); ?> </li>
     <li><?php echo $html->link(__('New Post', true), array('controller' => 'posts', 'action' => 'add')); ?> </li>
    </ul>
</div>

The problem is after i press Submit the values remains in name and content fields. Can anybody help me?

Thanks,

+2  A: 

You've got a couple of options here:

You can redirect after submitting, in your controller, after handling the $this->save method, place:

$this->redirect(array('action'=>'index'));

where the action is where you wish to return to.

Or you can clear the values, again in the controller, after $this->save

$this->data['Comment']['name'] = "";
etc...
CJD
Redirecting is definitely the way to go, as it avoids the user inadvertently re-submitting the comment if they reload the page.
Ben James
A: 

Inside your Comments controller make sure your add function is redirecting after doing $this->Comment->save($data);

Add this after making sure save() worked:

$this->flash('Thanks for the comment',array('controller'=>'comments','action'=>'index'));

Edit

Use the ajax helper $ajax->create and $ajax->submit. ie.

$ajax->submit('Add comment', array('update' => 'refreshArea','indicator' =>
'loading','complete' => 'document.commentForm.reset()'));
pcp
My redirecting is working after doing $this->Comment->save($data);.My problem is that the form keeps the old values. So instead to render 'add_succes' I should render the form again?
Andrei
Ahhh in the original question you didnt mention you were using the $ajax helper, this is happening to you for the following reasons:-you should be using $ajax->form, instead of $form->create()-When using $ajax->submit at the end of your form you should add: 'complete' => 'document.commentForm.reset()' this way you can reset the form after receiving the response.
pcp
A: 

my add action in comment controller looks like that:

function add() {
 if (!empty($this->data)) {
  if ($this->Comment->save($this->data)) {
   $comments = $this->Comment->find('all', array('conditions' => array('post_id' => $this->data['Comment']['post_id']), 'recursive' => -1));
  $this->data = $this->Comment->create();
  $this->set(compact('comments'));
  $this->render('add_succes','ajax');
  } else { $his->render('add_failure','ajax');}
 }
}

I use ajax to render the comments again. My problem is that in the comment form the old values still remain instead of erase them

Andrei
A: 

According to http://book.cakephp.org/view/1366/form, you should not use the $ajax->form() and $ajax->submit() in the same form.

So what now?

vanessa
A: 

$this->flash('Thanks for the comment'array('controller'=>'comments','action'=>

need help

Gurbet Sohbet