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

(This post was last modified: 05-01-2017, 02:49 AM by wolfgang1983.)

On my view I have a form_dropdown with some options


PHP Code:
<div class="row">
<
div class="col-sm-12">
<
div class="form-group">
<
label for="input_country">Country</label>
<?
php 

$options
[] = array();

foreach (
$countries as $key => $value) {       
$options
['0'] = '--- Select Country ---';
$options[$value['iso_code_2']] = $value['name'];
}

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

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


The iso code for Australia is AU

Question: If there is a error on form and have selected country how can I make sure it stays on the country so do not have to go through list again


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');
}

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

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

}



Attached Files
.php   Register.php (Size: 1.58 KB / Downloads: 102)
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
If form error how to keep on selected option in form_dropdown - by wolfgang1983 - 05-01-2017, 02:48 AM



Theme © iAndrew 2016 - Forum software by © MyBB