Andhika R.K.
Andhika R.K.

Reputation: 436

Set_value() Array fields select option Codeigniter not working

I trying to create dynamic server side select input, after i submit, the set_value('nilai[]') not showing any value.

Here's my Controller below:

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

    $this->form_validation->set_rules('nilai[]', 'Nilai Pantuhir', 'required');

    if ($this->form_validation->run() == FALSE)
    {
      $this->load->view('pantuhir/pantuhir_form');
    } else {
      $list_pantuhir = $this->input->post('nilai');

      foreach ($list_pantuhir as $key => $value) {
        echo $value."<br />";
      }
    }

Here's my view below :

    <div class="form-group <?php if(form_error('nilai[]')){echo 'has-error';} ?>">
      <select class="form-control" name="nilai[]">
        <option value="">- Choose-</option>
        <option value="<?php echo $rowPerson['intUserId'].'-'.'A';?>" <?php if(set_value('nilai[]') == $rowPerson['intUserId'].'-'.'A') { echo 'selected'; } ?>>A</option>
        <option value="<?php echo $rowPerson['intUserId'].'-'.'B';?>" <?php if(set_value('nilai[]') == $rowPerson['intUserId'].'-'.'B') { echo 'selected'; } ?>>B</option>
     </select>
     <?php echo form_error('nilai[]'); ?>
    </div>

I want to show set_value and get selected in option field if validation not correct.

Upvotes: 0

Views: 1375

Answers (2)

Pradeep
Pradeep

Reputation: 9717

Hope this will help you :

Use set_select instead of set_value. If you use a menu, this function permits you to display the menu item that was selected, after the form validation throws any error

It should be like this :

<div class="form-group <?php if(form_error('nilai[]')){echo 'has-error';} ?>">
    <select name="nilai[]" >
      <option value="" >---Choose----</option>
      <option 
              value="<?=$rowPerson['intUserId'].'-A';?>" 
              <?=set_select('nilai[]', $rowPerson['intUserId'].'-A');?>
      >A</option>
      <option 
              value="<?php echo $rowPerson['intUserId'].'-B';?>" 
              <?=set_select('nilai[]', $rowPerson['intUserId'].'-B');?> 
      >B</option>
    </select>
<?php echo form_error('nilai[]'); ?>
</div>

For more : https://www.codeigniter.com/user_guide/helpers/form_helper.html#set_select

Upvotes: 1

AbdulAhmad Matin
AbdulAhmad Matin

Reputation: 1146

Use this code in the view

<div class="form-group <?php if(form_error('nilai[]')){echo 'has-error';} ?>">
  <select class="form-control" name="nilai[]">
    <option value="">- Choose-</option>
    <option value="<?php echo $rowPerson['intUserId'].'-'.'A';?>" <?php if(set_value('nilai[]',rowPerson['intUserId'].'-'.'A') == $rowPerson['intUserId'].'-'.'A') { echo 'selected'; } ?>>A</option>
    <option value="<?php echo $rowPerson['intUserId'].'-'.'B';?>" <?php if(set_value('nilai[]',$rowPerson['intUserId'].'-'.'B') == $rowPerson['intUserId'].'-'.'B') { echo 'selected'; } ?>>B</option>
 </select>
 <?php echo form_error('nilai[]'); ?>
</div>

Upvotes: 0

Related Questions