Replication of checkboxes in Codeigniter after form validation failed

I have a problem re-dialing a set of checkboxes after an unsuccessful validation of the form returns the user back to the same form. Drop-down menus and text inputs can be classified, but not flags!

Here is a snippet of code for the flags:

<td> <?php echo form_checkbox('ambience[]', 'casual', set_checkbox('ambience[]', 'casual')); ?> Casual <br /> <?php echo form_checkbox('ambience[]', 'romantic', set_checkbox('ambience[]', 'romantic')); ?> Romantic <br /> <?php echo form_checkbox('ambience[]', 'outdoor', set_checkbox('ambience[]')); ?> Outdoor / Alfresco <br /> <?php echo form_checkbox('ambience[]', 'trendy', set_checkbox('ambience[]')); ?> Hip & Trendy <br /> <?php echo form_checkbox('ambience[]', 'vibrant', set_checkbox('ambience[]')); ?> Vibrant <br /> <?php echo form_checkbox('ambience[]', 'up_scale', set_checkbox('ambience[]')); ?> Upscale <br /> </td> 

A snippet of code for entering text that is successfully populated:

 <?php echo form_dropdown('price_range', $options, set_value('price_range')); ?> 

Any ideas? I'm really confused why set_checkbox doesn't work as advertised.

+7
source share
8 answers

You are mistaken in set_checkbox calls. When you use an array like "ambience []" in form_checkbox, you do not want to include square brackets ([]) in your set_checkbox call. Another problem is that set_checkbox requires a second parameter, which you only included in the first 2 checkboxes.

The set_checkbox value should be like this:

 set_checkbox('ambience', 'value'); 

Where "value" is the second parameter to the form_checkbox call. Like this:

 form_checkbox('ambience[]', 'value', set_checkbox('ambience', 'value')); 
+12
source

For the set_checkbox function to work correctly, the correct validation rule must be used for this element. I ran into this problem and could not get the value to show on resubmission until I turned it on:

 $this->form_validation->set_rules('checkbox_name', 'checkbox_title', 'trim'); 

Then everything worked perfectly.

+21
source

I really found that it only works if you use like this:

 form_checkbox('ambience[]', 'value', set_checkbox('ambience[]', 'value')); 

You need square brackets for the name to work correctly.

+4
source

Here is a working example. You must include the array name with brackets [] in each of $this->form_validation->set_rules() , form_checkbox() and set_checkbox() .

In the controller:

  $this->load->library('form_validation'); $this->form_validation->set_rules('set_reminder_days[]', 'Reminder Day', 'trim'); if( $this->form_validation->run() == FALSE ) //Field validation failed. { //form validation errors will show up automatically } else //Validation success. { //This is an array of all checked checkboxes $reminder_days = $this->input->post('set_reminder_days'); } 

In view:

  $day_options = array( 'S' => 'Sunday', 'M' => 'Monday', 'T' => 'Tuesday', 'W' => 'Wednesday', 'Th' => 'Thursday', 'F' => 'Friday', 'Sa' => 'Saturday' ); foreach( $day_options as $key => $day_option ) { echo form_checkbox('set_reminder_days[]', $key, set_checkbox('set_reminder_days[]', $key), 'class="form-checkbox"'); } 
+3
source
 function set_checkbox_array($field, $value) { $arr = isset($_POST[$field]) ? $_POST[$field] : FALSE; return ($arr !== FALSE && is_array($arr) && in_array($value, $arr)) ? 'checked="checked"' : ''; } 
0
source

I realized that set_checkbox accepts 3 parameters:

  set_checkbox(string $checkboxname, string $value, boolean $isChecked); 

For example:

  echo form_checkbox('mycbx[]', $item['id'], set_checkbox('mycbx[]', $item['id'], false) ); 

or as follows:

 $checkbox = array( 'name' => 'mycbx[]', 'value' => $item['id'], 'checked' => set_checkbox('mycbx[]', $item['id'], false) ); echo form_checkbox($checkbox); 
0
source

This is not used by the form helper. I am trying to use this code.

 <input type="checkbox" name="tes_display" value="1" <?php echo set_checkbox('tes_display', '1', FALSE); ?> /> 
0
source

I tried all the solutions in which no one worked. Therefore, I collected all the data, packed it into an array, and then used a loop to check if the values ​​in the array match the value of the selected field, if so, change the checked attribute to check.

Here is the html code:

 <div class="col-sm-10 tags"> <label> <input class="tags" type="checkbox" name="tags[]" value="love" >Love </label> <label> <input class="tags" type="checkbox" name="tags[]" value="God" >God </label> <label> <input class="tags" type="checkbox" name="tags[]" value="Reality" >Reality </label> <label> <input class="tags" type="checkbox" name="tags[]" value="Entrepreneurship">Entrepreneurship </label> </div> 

Here is the javascript code in the function

 (function(){ var tag_string = '<?php echo $_post['tags']; ?>', tags = tag_string.split(', '); boxes = document.getElementsByClassName('tags'); for(i = 0; i< boxes.length;i++ ){ if(tags.toString().includes(boxes[i].value)){ boxes[i].checked = "checked"; } } })(); 
0
source

All Articles