如何在codeigniter中重新填充表单

时间:2016-10-09 12:40:07

标签: php forms codeigniter

我正在尝试重新填充codeigniter表单,当我单击提交按钮时,表单输入消失。我试图使用设置值函数,但它无法正常工作,我自动加载表单助手。

表单视图

 <?php $attributes= array('id'=>'registration_form','class'=>'registration_form');?>

<div class="row">
    <div class="large-4 large-offset-8 columns">
<?php if($this->session->flashdata('reg_errors')):?>

<?php echo $this->session->flashdata('reg_errors');?>

<?php endif;?>
  </div>
</div>


<?php echo form_open('User/registration_check',$attributes);?>

<div class="registration_form">
  <div class="row">
    <div class="large-2 large-offset-8 columns">

<?php 

$data = array(

'class'=>'form-control',
'id'=>'fname',
'name'=>'fname',
'placeholder'=>'First Name',
'value'=> set_value('fname')


);

echo form_input($data);
?>

    </div>
       <div class="large-2 columns">

<?php 

$data = array(

'class'=>'form-control',
'name'=>'lname',
'placeholder'=>'Last Name'


);

echo form_input($data);
?>

    </div>
</div>
</div>
  <div class="row">
    <div class="large-4 large-offset-8 columns">
<?php 

$data = array(

'class'=>'form-control',
'name'=>'email',
'placeholder'=>'Email'


);

echo form_input($data);
?>
    </div>

</div>
  <div class="row">
    <div class="large-4 large-offset-8 columns">

<?php 

$data = array(

'class'=>'form-control',
'name'=>'cemail',
'placeholder'=>'Confirm Email'


);

echo form_input($data);
?>

    </div>

</div>
  <div class="row">
    <div class="large-4 large-offset-8 columns">

<?php 

$data = array(

'class'=>'form-control',
'name'=>'password',
'placeholder'=>'Password'


);

echo form_input($data);
?>

    </div>

</div>
  <div class="row">
    <div class="large-4 large-offset-8 columns">

<?php 


$data = array(

'class'=>'form-control',
'name'=>'cpassword',
'placeholder'=>'Confirm Password'


);

echo form_input($data);
?>

    </div>



 <div class="large-1 large-offset-8 columns">
<?php echo "<select name='day' id='day'><option value='default'>day</option>";
for($i=1; $i<32; $i++){
    echo "<option value='$i'".($i==$_POST["day"] ? " selected" : null).">$i</option>";
}
echo "</select>";?>
    </div>
<div class="medium-1 columns">
  <?php
echo "<select name='month' id='month'><option value='default'>month</option>";
for($i=1; $i<13; $i++){
    echo "<option value='$i'".($i==$_POST["month"] ? " selected" : null).">$i</option>";
}
echo "</select>";
?>
    </div>
    <div class="medium-2 columns">

<?php
echo "<select name='year'id='year'><option value='default' >year</option>";
for($i=1900; $i<2016; $i++){
    echo "<option value='$i'".($i==$_POST["year"] ? " selected" : null).">$i</option>";
}
echo "</select>";
?>
    </div>
  <div class="row">
    <div class="large-3 large-offset-9 columns">
   <div class="row">
    <div class="large-2 large-offset-2 columns">
<?php

$data = array(

'class'=>' success button',
'name'=>'register',
'id'=>'register',
'value'=>'Register'


);?>
<?php echo form_submit($data);?>

 </div>
 </div>
    </div>

</div>
<?php echo form_close();?>

用户控制器

class User extends CI_Controller {



    public function index()
    {

        $this->load->view('templates/head');
        $this->load->view('templates/home_header');
        $this->load->view('webPages/home');
        $this->load->view('templates/footer');
    }


          public function registration_check() {


    $this->form_validation->set_rules('email','Email','trim|required|valid_email|is_unique[users.user_email]');
    $this->form_validation->set_rules('cemail','Confirm Email','trim|required|matches[email]');
    $this->form_validation->set_rules('fname','Firstname','trim|required|min_length[5]');
    $this->form_validation->set_rules('lname','Lastname','trim|required|min_length[5]');
    $this->form_validation->set_rules('password','Password','trim|required|min_length[5]');
    $this->form_validation->set_rules('cpassword','Confirm Password','trim|required|min_length[5]|matches[password]');
    $this->form_validation->set_message('is_unique','email already in use'); 


     if($this->form_validation->run()) {



        //generates a random key 
        $key = md5(uniqid());

        $this->load->library('email',array('mailtype'=>'html'));
        $this->load->model('User_model');
        $this->email->from('c3392262@joshpercival.co.uk',"joshua percival");
        $this->email->to($this->input->post('email'));
        $this->email->subject("Account Confirmation");

        $message = "<p>Thank you for signing up</p>";
        $message .= "<p><a href ='".base_url()."index.php/Users/register_user/$key'>Click here</a> to confirm your account</p>";

        $this->email->message($message);

        if( $this->User_model->add_temp($key)) {

        if($this->email->send()){
       $data['title'] = 'Confirmation Email Sent';
       $email['email'] = $this->input->post('email');
       $this->load->view('templates/header',$data);
       $this->load->view('authentication/email_sent_success',$email);
       } else {
           echo 'email was not sent';
       }
        } else {
            echo 'could not add to database';
        }


        //add to temp database 


    } else {
          $data = array(

            'reg_errors'=> validation_errors()
            );
               $this->session->set_flashdata($data);
         redirect('User/index');


    }


    }

1 个答案:

答案 0 :(得分:0)

问题在于此代码块

} 
else 
{
  $data = array('reg_errors'=> validation_errors());
  $this->session->set_flashdata($data);
  redirect('User/index');
}

您不能以这种方式使用带有form_validation的重定向,并保留您一直使用的form_validation实例。

通过重定向,服务器会丢弃您当前的CI实例(以及所有已加载的类 - 包括form_validation),然后将创建一个全新的CI实例。 set_value()函数使用form_validation->run()收集的信息,但这已经很久了。它没有用于重新填充表单的信息。

再看一遍Form Validation Tutorial并注意没有涉及redirect电话。