0
votes

I am trying to repopulate a codeigniter form, when I click the submit button the form input disappears. I have tried to use the set value function but its not working, I am auto loading the form helper.

form view

 <?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();?>

user controller

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('[email protected]',"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
Please edit your question and include the controller User and the User::index() code. - DFriend
iv added my user controller - thomaSmith
Does the form fail to reappear when you have purposely posted with invalid fields? Or does it fail to reappear when you provide what should be valid fields? Or, does it fail no matter what state the fields are in? - DFriend
when the submit button is pressed the values that are in the form, disappear. - thomaSmith

1 Answers

0
votes

The problem is in this block of code

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

You cannot use redirect with form_validation in this way and retain the instance of form_validation you have been using.

With the redirect the server discards your current instance of CI (and all loaded classes - including form_validation) and then will create a whole new instance of CI. The set_value() function uses information gathered by form_validation->run() but that is long gone. It has no information to use to repopulate the forms.

Take another very close look at the Form Validation Tutorial and notice there is no redirect call involved.