开发者

Cakephp comment form input keeps the same value after i press add

开发者 https://www.devze.com 2022-12-13 20:19 出处:网络
I\'ve created a blog site from \"Beginning CakePHP from novice to professiona\"-David Golding. I have the comment view listed below:

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 h开发者_开发技巧elp me?

Thanks,


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...


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


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

So what now?


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()')); 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号