user3142334
user3142334

Reputation: 53

retaining data entered by user in the form

I am using codeigniter. Below is the code for my registration page.

Controller

function register()
     {
         $this->load->library('form_validation');
      $this->form_validation->set_rules('first_name', 'First Name',     'trim|required');
          $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
          $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|is_unique[users.username]');
          $this->form_validation->set_rules('user_email', 'Email', 'trim|required|valid_email|is_unique[users.user_email]');
          $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
           $this->form_validation->set_rules('ans_1', 'Security answer 1', 'required');      
              $this->form_validation->set_rules('ans_2', 'Security answer 2',     'required'); 
          if($this->form_validation->run() == FALSE) { 
            //not validated - reload the view and display errors 
               $this->load->database();

            $this->load->model('user'); 
            $data['ques'] = $this->user->get_que();
            $this->load->view('register',$data); 

          } 
            else { 
             $this->load->database();

            $this->load->model('user'); 
            //create user 
            $this->user->create_user();
            $this->thank();
            } 
     }



View

 <!DOCTYPE html> 
<html>
 <head>
   <title> Login</title>
   <link rel="stylesheet" href="http://localhost/cinema/assets/css/form.css">

 </head>
 <body>




            <form action="http://localhost/cinema/login/register" method="post" accept-charset="utf-8" class="username register" >
           <h3>User Registration form:</h3>
            <p><label for="first_name">First Name</label>
                <input type="text" name="first_name" /></p>
             <p><label for="last_name">Last Name</label>
                <input type="text" name="last_name" /></p>

            <p><label for="username">Username</label>
                <input type="text" name="username" /></p>

           <p><label for="user_email">Email</label>
                <input type="text" name="user_email"  /></p>

     <p><label for="password">Password</label>
             <input type="password"   name="password"  /></p>
         <br><font size='4'>Select security question 1:</font>
           <select name="que_1">
        <?php 
            foreach($ques as $que)
        { 
          echo '<option value="'.$que->que_ID.'">'.$que->que.'</option>';
        }
        ?>
        </select><br><br>
         <p><label for="ans_1">Answer:</label>
                <input type="text" name="ans_1"  /></p>
         <br><font size='4'>Select security question 2:</font>
           <select name="que_2">
        <?php 
            foreach($ques as $que)
        { 
          echo '<option value="'.$que->que_ID.'">'.$que->que.'</option>';
        }
        ?>
        </select><br><br>
         <p><label for="ans_2">Answer:</label>
                <input type="text" name="ans_2"  /></p>

         <input type="submit" name="btnSubmit" class="styled-button-8"  style="float:right; margin-right:300px; margin-top:-10px;" value="Sign Up!"
             />
            <font color="red" size="4"><b>
         <?php echo validation_errors(); ?></b></font>
           </form>
 </body>
</html> 

   <br>

My problem is that in case the user fills invalid data and clicks the signup button, the page is refreshed and the validation errors are displayed but the data entered by the user doesn't remain there.He has to again fill the whole form. I want the user data to be retained and displayed in this case. Thanks in advance .. :)

Upvotes: 0

Views: 1761

Answers (4)

The Odd Developer
The Odd Developer

Reputation: 929

In your controller put a data array and pass the user entered values to that.Then if there are form validation errors pass those data to the view.

In the view use CI set_value() method to show the previous entered values.

Upvotes: 0

Kumar V
Kumar V

Reputation: 8838

Better solution is : use post data

<input type="text" name="last_name" value='<?php echo $this->input->post('last_name')?>'/>

When you use set_value(), you must send that field to validation even it is not required. But for Post, you don't want to do any additional work.

Upvotes: 0

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

For this you need set_value()

For input fileds

<input type="text" name="last_name" value='<?php echo set_value('last_name')?>'/>

This will get the value from $_POST and print after failed validation. Also takes 2nd parameter as default value.

<?php echo set_value('last_name','John')?>

Read the Form Helper Class.

Upvotes: 1

Balaji Kandasamy
Balaji Kandasamy

Reputation: 4506

You can try like this. You can maintain session to display user fields data while validation check. I added a Sample with session in your form.

Your View :

<?php session_start(); ?>
<form action="http://localhost/cinema/login/register" method="post" accept-charset="utf-8" class="username register" >
    <h3>User Registration form:</h3>
    <p><label for="first_name">First Name</label>
        <input type="text" name="first_name" value="<?php echo $_SESSION['first_name']; ?>" /></p>
    <font color="red" size="4"><b>
        <?php echo validation_errors(); ?></b></font>
</form>

Your controller:

function register() {
    $this->load->library('form_validation');
    $firstName = $this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
    $this->session->set_userdata('first_name', $firstName);
    if ($this->form_validation->run() == FALSE) {
        //redirect to register page
    } else {
        //register user details
        $this->session->unset_userdata('first_name');
    }

Upvotes: 0

Related Questions