开发者

How do I preserve form values?

I have a controller which loads the view like this:

    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.
        }
    }
开发者_StackOverflow社区


Here is how to preserve the form fields when dealing with errors...

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

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

This is how to re-populate the HTML form...

<?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" />

For more information look over here - Form Validation in Codeigniter Look for the heading Re-populating the form


in function simpleForm add default variables so, after validation you can call it again with userform values

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 
      }
    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜