metaphor
metaphor

Reputation: 470

Codeigniter form validation - forms value always disappeared

I'm making a form input in codeigniter with validation. The form validation work fine (if all form are empty), but if i give the value only in 1 or 2 form (there is an empty form) then click the submit(save) button, the value in form always disappeared.

My Controller-function Save

function save($is_update=0) {

        $data['nik']            = $this->input->post('nik', true);
        $data['nmdepan']        = $this->input->post('nmdepan', true);
        $data['nmlengkap']      = $this->input->post('nmlengkap', true);
        $data['username']       = $this->input->post('username', true);
        $data['password']       = $this->input->post('password', true);
        $data['leveluserid']    = $this->input->post('leveluserid', true);
        $data['jabid']          = $this->input->post('jabid', true);

        $this->_set_rules();
        if ($this->form_validation->run() == true) {
            if($is_update==0) {
                if($this->M_menu_user->insert($data))
                    redirect('master/user/C_menu_user');
            } else {
                $id = $this->input->post('id');
                if($this->M_menu_user->update_by_id($data, $id))
                    redirect('master/user/C_menu_user');
            }
        }
    }

Controller-function _set_rules

function _set_rules() {
        if($this->session->userdata('logged_in')) {
            $session_data            = $this->session->userdata('logged_in');
            $data['username']        = $session_data['username'];
            $data['password']        = $session_data['password'];
            $data['jabid']           = $session_data['jabid'];
            $data['leveluserid']     = $session_data['leveluserid'];

            $LevelUser  = $session_data['leveluserid'];
            $username   = $session_data['username'];

            $menus = $this->M_menu->menus($LevelUser);
            $data2 = array('menus' => $menus);

            $this->load->library('form_validation');


            $this->form_validation->set_rules('nik','NIK','required|max_length[10]');
            $this->form_validation->set_rules('nmdepan','Nama Depan','required|max_length[30]');
            $this->form_validation->set_rules('nmlengkap','Nama Lengkap','required|max_length[30]');
            $this->form_validation->set_rules('username','Username','required|max_length[15]');
            $this->form_validation->set_rules('password','Password','required|max_length[30]');
            $this->form_validation->set_rules('leveluserid','Level ID User','required|max_length[10]');
            $this->form_validation->set_rules('jabid','ID Jabatan','required|max_length[10]');

            $this->form_validation->set_message('required', '%s <font color="red"><strong>Harus Diisi.</strong></font>');

            if ($this->form_validation->run() == FALSE) {
                $this->data['is_update'] = 0;
                $this->load->view('master/user/V_menu_form_user', array_merge($data, $data2,$this->data));
            } else {
                $this->data['is_update'] = 0;
                $this->load->view('master/user/V_menu_form_user', array_merge($data, $data2,$this->data));

            }
        } else {

                redirect('C_login', 'refresh');
        }       
    }

Here is my view

<?php
  $this->load->view('template/head');
?>

<!--custom css-->
<!-- iCheck -->
<link href="<?php echo base_url('assets/AdminLTE-2.0.5/plugins/iCheck/flat/blue.css') ?>" rel="stylesheet" type="text/css" />


<?php
  $this->load->view('template/topbar');
  $this->load->view('template/sidebar');
?>


<!-- Content Header (Page header) -->
<!-- Main content -->
<section class="content">
<?php  

    if(!empty($query)) {
        $row = $query->row_array();
    } else {

        $row['userid']      = '';
        $row['nik']     = '';
        $row['nmdepan']     = '';
        $row['nmlengkap']   = '';
        $row['username']    = '';
        $row['password']    = '';
        $row['leveluserid']     = '';
        $row['jabid']       = '';
    }

    echo "<div class='row'>
            <div class='col-md-12'>
                <div class='box box-primary'>
                    <div class='box-header'>
                        <h3 class='box-title'>FORM USER</h3>
                    </div>                

                        <div class='box-body'>";

                            echo form_open('master/user/C_menu_user/save/'.$is_update);

                            echo form_hidden('id',$row['userid']);

                            echo "<div class='row'>
                                    <div class='col-lg-12'>
                                        <div class='form-group'>";
                                        echo "<label class='col-sm-2 control-label'>NIK</label>
                                              <label class='col-sm-1 control-label'>:</label>
                                                <div class='col-sm-3'>".form_input('nik',$row['nik'],"maxlength='10' class=form-control").form_error('nik'). "</div>";
                                    echo "</div>
                                </div>
                            </div> <br>";

                            echo "<div class='row'>
                                    <div class='col-lg-12'>
                                        <div class='form-group'>";
                                        echo "<label class='col-sm-2 control-label'>Nama Depan</label>
                                              <label class='col-sm-1 control-label'>:</label>
                                                <div class='col-sm-3'>".form_input('nmdepan',$row['nmdepan'],"maxlength='30' class=form-control").form_error('nmdepan'). "</div>";
                                    echo "</div>
                                </div>
                            </div> <br>";

                            echo "<div class='row'>
                                    <div class='col-lg-12'>
                                        <div class='form-group'>";
                                        echo "<label class='col-sm-2 control-label'>Nama Lengkap</label>
                                              <label class='col-sm-1 control-label'>:</label>
                                                <div class='col-sm-3'>".form_input('nmlengkap',$row['nmlengkap'],"maxlength='30' class=form-control").form_error('nmlengkap'). "</div>";
                                    echo "</div>
                                </div>
                            </div> <br>";

                            echo "<div class='row'>
                                    <div class='col-lg-12'>
                                        <div class='form-group'>";
                                        echo "<label class='col-sm-2 control-label'>Username</label>
                                              <label class='col-sm-1 control-label'>:</label>
                                                <div class='col-sm-3'>".form_input('username',$row['username'],"maxlength='15' class=form-control").form_error('username'). "</div>";
                                    echo "</div>
                                </div>
                            </div> <br>";

                            echo "<div class='row'>
                                    <div class='col-lg-12'>
                                        <div class='form-group'>";
                                        echo "<label class='col-sm-2 control-label'>Password</label>
                                              <label class='col-sm-1 control-label'>:</label>
                                                <div class='col-sm-3'>".form_input('password',$row['password'],"maxlength='30' class=form-control").form_error('password'). "</div>";
                                    echo "</div>
                                </div>
                            </div> <br>";

                            echo "<div class='row'>
                                    <div class='col-lg-12'>
                                        <div class='form-group'>";
                                        echo "<label class='col-sm-2 control-label'>Level ID User</label>
                                              <label class='col-sm-1 control-label'>:</label>
                                                <div class='col-sm-3'>".form_input('leveluserid',$row['leveluserid'],"maxlength='10' class=form-control").form_error('leveluserid'). "</div>";
                                    echo "</div>
                                </div>
                            </div> <br>";

                            echo "<div class='row'>
                                    <div class='col-lg-12'>
                                        <div class='form-group'>";
                                        echo "<label class='col-sm-2 control-label'>ID Jabatan</label>
                                              <label class='col-sm-1 control-label'>:</label>
                                                <div class='col-sm-3'>".form_input('jabid',$row['jabid'],"maxlength='10' class=form-control").form_error('jabid'). "</div>";
                                    echo "</div>
                                </div>
                            </div> <br>";

                            echo form_submit('btn_save','Simpan',"class='btn btn-primary'");

                            echo "<a href=".site_url('master/user/C_menu_user')." class='btn btn-success'>Kembali</a>";


                           echo form_close();
                        echo "</div>

                </div>
            </div>
          </div>";  

?>

</section><!-- /.content -->
<?php
$this->load->view('template/js');
?>

<!--custom js -->
<!-- Sparkline -->
<script src="<?php echo base_url('assets/AdminLTE-2.0.5/plugins/sparkline/jquery.sparkline.min.js') ?>" type="text/javascript"></script>

<?php
$this->load->view('template/foot');
?>

look at this pictures, i've filled in 3 form. enter image description here

Then i click button "Simpan", the form value will be disappear. (Simpan = Save/Submit and Kembali = Back/Cancel). So i need to start again fill the forms. enter image description here

The validation work fine if all form are empty. enter image description here

How to keep the value stay in form ? did i make a wrong form validation ?

Thanks!

Upvotes: 1

Views: 2301

Answers (2)

devmorgue
devmorgue

Reputation: 13

You need to include - set_value('name'); - so the value gets set after submit. When the form returns with errors it will repopulate the input fields where you have set the value.

http://www.codeigniter.com/user_guide/helpers/form_helper.html#set_value

Upvotes: 0

Noob
Noob

Reputation: 154

add value as a second para

$value= '$this->input->post('form name')'

Also why are you loading your views in your view? you should use a controller? and you should not really save your password into your session.

Example of using forms in Codeigniter are

form_input([$data = ''[, $value = ''[, $extra = '']])
Parameters: 
$data (array) – Field attributes data
$value (string) – Field value
$extra (mixed) – Extra attributes to be added to the tag either as an array or a literal string
Returns:    
An HTML text input field tag

I personally would use an array tho

$data = array(
        'name'          => 'username',
        'id'            => 'username',
        'value'         => 'johndoe',
        'maxlength'     => '100',
        'size'          => '50',
        'style'         => 'width:50%'
);

echo form_input($data,  $value= '$this->input->post('form name')' );

Takea look at this website https://www.codeigniter.com/userguide3/helpers/form_helper.html

Upvotes: 1

Related Questions