Welcome Guest, Not a member yet? Register   Sign In
If form error how to keep on selected option in form_dropdown
#2

I think I have found my solution 

Because I have placed this on the controller 

PHP Code:
if ($this->input->post('country_id')) 
 
{
$data['country_id'] = $this->input->post('country_id');
} else {
$data['country_id'] = '';



All I need to do now is on the view in the third section


PHP Code:
echo form_dropdown('country_id'$options$country_id, array('class' => 'form-control''id' => 'input_country')); 


I think I have it correct now

PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class 
Register extends CI_Controller 
{

    public function 
__construct() 
    {
        
parent::__construct();
        
$this->load->model('localisation/country_model');
        
$this->load->library('form_validation');
    }

    public function 
index()
    {
        if ((
$this->input->server("REQUEST_METHOD") == 'POST') && $this->validate()) 
        {
            
// Success form area
        
}

        if (isset(
$this->error['username'])) 
        {
            
$data['error_username'] = $this->error['username'];
        } else {
            
$data['error_username'] = '';
        }

        if (isset(
$this->error['password'])) 
        {
            
$data['error_password'] = $this->error['password'];
        } else {
            
$data['error_password'] = '';
        }

        if (isset(
$this->error['firstname'])) 
        {
            
$data['error_firstname'] = $this->error['firstname'];
        } else {
            
$data['error_firstname'] = '';
        }

        if (isset(
$this->error['lastname'])) 
        {
            
$data['error_lastname'] = $this->error['lastname'];
        } else {
            
$data['error_lastname'] = '';
        }

        if (isset(
$this->error['email'])) 
        {
            
$data['error_email'] = $this->error['email'];
        } else {
            
$data['error_email'] = '';
        }

        if (isset(
$this->error['country_id'])) 
        {
            
$data['error_country'] = $this->error['country_id'];
        } else {
            
$data['error_country'] = '';
        }

        if (
$this->input->post('username')) 
        {
            
$data['username'] = $this->input->post('username');
        } else {
            
$data['username'] = '';
        }

        if (
$this->input->post('firstname')) 
        {
            
$data['firstname'] = $this->input->post('firstname');
        } else {
            
$data['firstname'] = '';
        }

        if (
$this->input->post('lastname')) 
        {
            
$data['lastname'] = $this->input->post('lastname');
        } else {
            
$data['lastname'] = '';
        }

        if (
$this->input->post('email')) 
        {
            
$data['email'] = $this->input->post('email');
        } else {
            
$data['email'] = '';
        }

        if (
$this->input->post('password')) 
        {
            
$data['password'] = $this->input->post('password');
        } else {
            
$data['password'] = '';
        }

        
$data['countries'] = $this->country_model->get_countries();

        if (
$this->input->post('country_id')) 
        {
            
$data['country_id'] = $this->input->post('country_id');
        } else {
            
$data['country_id'] = '';
        }

        
$this->load->view('default/template/common/header'$data);
        
$this->load->view('default/template/account/register'$data);
        
$this->load->view('default/template/common/footer'$data);
    }

    public function 
validate() 
    {
        
// Set the form validation rules

        
$this->form_validation->set_rules('username''username''required');
        
$this->form_validation->set_rules('firstname''firstname''required');
        
$this->form_validation->set_rules('lastname''lastname''required');
        
$this->form_validation->set_rules('email''email''required');
        
$this->form_validation->set_rules('password''password''required');

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

            if (
form_error('username')) 
            {
                
$this->error['username'] = form_error('username');
            }

            if (
form_error('email')) 
            {
                
$this->error['email'] = form_error('email');
            }

            if (
form_error('firstname')) 
            {
                
$this->error['firstname'] = form_error('firstname');
            }

            if (
form_error('lastname')) 
            {
                
$this->error['lastname'] = form_error('lastname');
            }

            if (
form_error('password')) 
            {
                
$this->error['password'] = form_error('password');
            }
        } 

        if (
$this->input->post('country_id') === '0') {
            
$this->error['country_id'] = 'You need to select your country';
        }

        return !
$this->error;
    }



Attached Files
.php   Register.php (Size: 3.59 KB / Downloads: 69)
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply


Messages In This Thread
RE: If form error how to keep on selected option in form_dropdown - by wolfgang1983 - 05-01-2017, 03:18 AM



Theme © iAndrew 2016 - Forum software by © MyBB