按add后,Cakephp注释表单输入保持相同的值
我已经创建了一个博客网站,从“从新手到职业”,David Golding。我的评论意见如下:按add后,Cakephp注释表单输入保持相同的值,php,forms,cakephp,input,Php,Forms,Cakephp,Input,我已经创建了一个博客网站,从“从新手到职业”,David Golding。我的评论意见如下: <div class="comments form"> <?php echo $form->create('Comment');?> <fieldset> <legend><?php __('Add Comment');?></legend> <?php echo $for
<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>
问题是在我按下Submit后,值仍保留在name和content字段中。
有人能帮我吗
谢谢,您在这里有几个选项: 在处理$this->save方法后,您可以在控制器中提交后重定向,放置:
$this->redirect(array('action'=>'index'));
您希望返回的操作所在的位置
或者,您可以在$this->save之后再次在控制器中清除这些值
$this->data['Comment']['name'] = "";
etc...
在Comments控制器中,确保在执行
$this->Comment->save($data)之后,add函数正在重定向代码>
在确保save()正常工作后添加:
编辑
使用ajax助手$ajax->create
和$ajax->submit
。即
$ajax->submit('Add comment', array('update' => 'refreshArea','indicator' =>
'loading','complete' => 'document.commentForm.reset()'));
我在comment controller中的添加操作如下所示:
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');}
}
}
我使用ajax再次呈现注释。我的问题是,在注释表单中,旧值仍然保留,而不是删除它们。根据,您不应该在同一表单中使用$ajax->form()
和$ajax->submit()
那么现在该怎么办呢?重定向无疑是一种方法,因为它可以避免用户在重新加载页面时无意中重新提交评论。在执行$this->comment->save($data);,之后,我的重定向工作正常;。我的问题是表单保留了旧的值。因此,如果要呈现“add_success”,我应该再次呈现表单?啊,在最初的问题中,您没有提到您正在使用$ajax助手,这种情况发生在您身上的原因如下:-您应该使用$ajax->form,而不是$form->create()-使用$ajax->在表单末尾提交时,您应该添加:“complete'=>”document.commentForm.reset(),这样您可以在收到响应后重置表单。您可以提供任何其他支持证据吗?
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');}
}
}