0
votes

I am trying to submit simple form data along with multiple checkbox options.

But on form validation, these checkbox options return unchecked.

View Code

  <?php echo form_checkbox( array('id' => 'check_passport', 'name' => 'purchaser_meta[]','value'=>'Passport','class'=>'form-check-input' )); ?> 
  <?php echo form_label('Passport','check_passport');?>
  <?php  echo form_error('resident_passport');  ?>

Controller Code

$this->load->library('form_validation'); 
$this->form_validation->set_rules('purchaser_meta','Purchaser Type Meta','trim|required');

Though, it catches the values submitted from these check boxes.

Like $this->input->post('purchaser_meta') returns the array of values submitted array(3) { [0]=> string(8) "Passport" [1]=> string(4) "Visa" [2]=> string(11) "ID" } .

Have a look on these images further.

Submitting form with options checked

enter image description here

After submitting form / form_validation occurred

enter image description here

Problem: I want these check boxes remain checked upon form validation errors. I don't know what i am missing here?

3
You need to assign $_POST data to $this->data["purchaser_meta"] and use this data in form to repopulate the form field after the validation. - sathya seelan

3 Answers

0
votes

When you submit form that time you have get value of checkbox in return array. So simple you have to just check through if...else..Condition for checked or unchecked.

$arrayval = array{"Passport","Visa","ID"};

if (in_array("Passport", $arrayval)){
  echo "Checked";
}

Put same code for all checkbox.

May be this things helps you.

0
votes

Try like this

<?php echo form_checkbox( array('id' => 'check_passport', set_checkbox('purchaser_meta[]','check'), 'name' => 'purchaser_meta[]','value'=>'Passport','class'=>'form-check-input' )); ?>
0
votes

//replace set_checkbox with this

set_checkbox('purchaser_meta', 'Passport', false)