Re-populate checkbox if it fails in validation in an edit form in Codeigniter

前端 未结 5 354
谎友^
谎友^ 2021-01-16 18:13

I works on the an data-edit form in codeigintier. And the problem is about re-populate checkbox

It works if it is an add form (that means I need not concern about th

相关标签:
5条回答
  • 2021-01-16 18:53

    Can give one suggestion?? 1. Hide all the checked value of checkbox in input box when you are directed towards edit page.

    1. If checked box is checked in edit page, edit the value of hidden input field of textbox value.

    2. Submit it, when validation failed, checked or repopulate the checkbox value according to hidden field value. send checkbox value of checked box field through array from controller to edit page view like this. e.g $data['repopulate_checks'] = $this->input->post('array name of checkboxs'); In view : getit like this $catch_checkbox = $repopulate_checks; You can directly get through $repopulate_checks also. Hope this help you.

    0 讨论(0)
  • 2021-01-16 18:54

    set_checkbox takes a third argument to set the default state, so basically you have to do something like this

    echo set_checkbox('is_default', 1, $customer_group[0]['is_default'] == "1"); 
    
    0 讨论(0)
  • 2021-01-16 19:00

    You can use form_checkbox() function: Guide

    $isChecked = False; // or True for default value
    

    If have stored data then:

    $isChecked = $customer_group[0]['is_default']; echo form_checkbox('input_name', 'value', $isChecked);

    or the hard way: set_checkbox():

    The first parameter must contain the name of the checkbox, the second parameter must contain its value, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE)

    <input type="checkbox" name="is_default" value="1" <?php echo ($customer_group[0]['is_default']) ? set_checkbox('is_default', '1') : '' ; ?>/>

    0 讨论(0)
  • 2021-01-16 19:09

    In order to re-populate checkbox following code might be helpful:

    set_checkbox('fieldName', 'fieldValue');
    

    Where 'value' is the second parameter of the form_checkbox call. Like this:

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

    Now if you are on edit form then below code might help you

    $getVal=$valFromDb; //$valFromDb is actually value of the filed from db as you are on edit page
    if($getVal!=0){
    {
       echo form_checkbox('fieldName[]', 'value', true);
    }
    else
    {
       echo form_checkbox('fieldName[]', 'value', false);
    }
    
    0 讨论(0)
  • 2021-01-16 19:11

    set_checkbox takes a third argument to set the default state, so basically you have to do something like this

    $checked = FALSE; if($customer_group[0]['is_default']){ $checked = TRUE; }

    echo set_checkbox('is_default', 1, $checked); 
    
    0 讨论(0)
提交回复
热议问题