php yii表单在提交后没有清除

时间:2014-10-31 11:23:26

标签: php forms yii

我在UserController.php中使用以下代码注册新用户

$model=new Users;

    if(isset($_POST['Users'])) {
        $model->attributes = $_POST['Users'];
        if ($model->register()) {               
            $model->setIsNewRecord(true);
            if ($model->save()) {
                Yii::app()->user->setFlash('info','User registered successfully!!! Please login to Continue.'); 
            } else {
                Yii::app()->user->setFlash('info','Sorry, an unexpected error has occurred!');
            }
        }   
    }
    $this->render('register',array('model'=>$model));

注册成功后,注册表格会保留输入的值。我该如何清除此表格?

以下是视图文件中的表单代码 - Register.php

<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'register-form',
    'enableClientValidation'=>true,
    'clientOptions'=>array(
        'validateOnSubmit'=>true,
    ),
)); ?>    
    <p class="note">Fields with <span class="required">*</span> are required.</p>           
    <div class="row">
        <?php echo $form->labelEx($model,'username'); ?>
        <?php echo $form->textField($model,'username'); ?>
        <?php echo $form->error($model,'username'); ?>
    </div>

-----other textfields in div similar to the above one is present here - not show due to space limit-----

<div id="info"> <font color="red"> <?php foreach(Yii::app()->user->getFlashes() as $key => $message) {
    echo '<div class="flash-' . $key . '">' . $message . "</div>\n";
} ?></font></div>
<div class="row buttons">
    <?php echo CHtml::submitButton('Register'); ?>
</div>

1 个答案:

答案 0 :(得分:5)

您可以清除以下模型属性:

$model->unsetAttributes();

将用户重定向到页面甚至是当前页面会更好。或者只是取消设置$_POST值。

例如(如果您不想重定向)

unset($_POST['Users']);

或更好:

$this->redirect('controller/action');
//or Yii::app()->request->redirect('controller/action');
Yii::app()->end();