如何保留表单值?

时间:2010-11-14 08:13:09

标签: php codeigniter

我有一个加载视图的控制器:

    class A extends Controller{
        public function simpleForm(){
          //this generates the form
        }

        public function simpleFormSubmitted(){
          // Simple form is submitted here. Here I perform validation and if
          // validation fails I want to display simpleform again with all the values
          // inputted by the user as it is and if validation succeeds I want to
          // redirect to some other page. I am using simple HTML to generate the
          // form and not the formhelper of CodeIgniter because I am more comfortable
          // with HTML rather than remembering the syntax of CodeIgniter.
        }
    }

2 个答案:

答案 0 :(得分:2)

以下是处理错误时如何保留表单字段...

$fields['username'] = 'Username';
$fields['password'] = 'Password';
$fields['passconf'] = 'Password Confirmation';
$fields['email'] = 'Email Address';

$this->validation->set_fields($fields);

这是重新填充HTML表单的方法......

<?php echo $this->validation->error_string; ?>

<?php echo form_open('form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="<?php echo $this->validation->username;?>" size="50" />

有关详细信息,请查看此处 - Form Validation in Codeigniter 查找标题Re-populating the form

答案 1 :(得分:0)

函数simpleForm中的

添加默认变量,因此在验证后您可以使用userform值再次调用它

class A{
        public function simpleForm($val1="", val2="", $val3=""){
          //this generates the form
        }

        public function simpleFormSubmitted(){
          //simple form is submitted here. Here is perform validation and if validation   fails i 
      if(!$valid){

          $result = $this->simpleForm($val1, $val2, $val3 etc...)
       }
      else{
      $result = //whatever you need to output after validation success
      }
      return $result 
      }
    }