CodeIgniter Forums
How to Keep maintain Radio button and chekbox Values After submit in form - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How to Keep maintain Radio button and chekbox Values After submit in form (/showthread.php?tid=61685)



How to Keep maintain Radio button and chekbox Values After submit in form - Chandini - 05-05-2015

How to keep maintain the values for radio buttons and checkbox , select tag values in registrer form in CI ...
My Code Is :





<tr><td>Address</td><td><?= form_textarea(array("name"=>"address","id"=>"address","value"=>set_value("address"),"rows"=>"8","cols"=>"22"))?></td><td id="error"><?= form_error("address") ?></td></tr>
<?php
$status = array("-1"=>"Select One",
                "Married"=>"Married",
               "Unmarried"=>"Unmarried",
"divorced"=>"Divorced")

?>




<tr><td>Marital Status</td><td ><?= form_dropdown("marital_status",$status,"-1",'style="width:175px;"') ?></td><td id="error"><?= form_error("marital_status") ?></td></tr>
<tr><td>Hobbies</td><td><?= form_checkbox("hobbies","Cricket") ?><span>Cricket</span>
                       <?= form_checkbox("hobbies","Chess")?><span>Chess</span>
                       <?= form_checkbox("hobbies","kabbadi")?><span>Kabbadi</span></td><td id="error"><?= form_error("hobbies")?></td></tr>


RE: How to Keep maintain Radio button and chekbox Values After submit in form - mwhitney - 05-06-2015

On your call to form_dropdown(), you're passing "-1" to the $selected parameter. If you pass the selected value (or an array of selected values if it supports multiple selections), it will select that value. If you don't pass a value to this parameter (or pass an empty array), it will attempt to automatically detect the selected value.

For each of your form_checkbox() calls, you should pass a third parameter, which should be set to true when that checkbox is checked, and false when it is not.

For radio buttons, form_radio() just sets the type to 'radio', then passes the parameters through to form_checkbox(), so they should be treated similarly to the checkboxes.


RE: How to Keep maintain Radio button and chekbox Values After submit in form - Johnny Stec - 05-06-2015

<input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" <?php echo set_radio('optionsRadios','option1',TRUE);?> />

TRUE it's only to tell the default selected one.

Hope it helps..


RE: How to Keep maintain Radio button and chekbox Values After submit in form - Chandini - 05-06-2015

(05-06-2015, 10:00 AM)Johnny Stec Wrote: <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" <?php echo set_radio('optionsRadios','option1',TRUE);?> />

TRUE it's only to tell the default selected one.

Hope it helps..

I am not like to give a by default values ... Whether user select on radio button that only selected... otherwise show error message like plz check one  ...  if employee is selected one radio button .. after submit  how to keep that value in radio buttons... ?  


RE: How to Keep maintain Radio button and chekbox Values After submit in form - Chandini - 05-06-2015

(05-06-2015, 08:22 AM)mwhitney Wrote: On your call to form_dropdown(), you're passing "-1" to the $selected parameter. If you pass the selected value (or an array of selected values if it supports multiple selections), it will select that value. If you don't pass a value to this parameter (or pass an empty array), it will attempt to automatically detect the selected value.

For each of your form_checkbox() calls, you should pass a third parameter, which should be set to true when that checkbox is checked, and false when it is not.

For radio buttons, form_radio() just sets the type to 'radio', then passes the parameters through to form_checkbox(), so they should be treated similarly to the checkboxes.

I am not like to give a by default values ... Whether user select on radio button that only selected... otherwise show error message like plz check one  ...  if employee is selected one radio button .. after submit  how to keep that value in radio buttons... ?  


RE: How to Keep maintain Radio button and chekbox Values After submit in form - mwhitney - 05-07-2015

Ignoring the HTML, here's an example of how to do this, assuming that you pass the selected value of marital_status to the view as $marital_status and the values of the checked checkboxes in an array as $hobbies.

PHP Code:
<?php

$selectedStatus 
= isset($marital_status) ? $marital_status '-1';
$selectedHobbies = isset($hobbies) ? $hobbies : array();

$status = array(
    "-1"        => "Select One",
    "Married"   => "Married",
    "Unmarried" => "Unmarried",
    "divorced"  => "Divorced",
);
$statusDropdown = array(
    'name'  => 'marital_status',
    'id'    => 'marital_status',
    'class' => 'status',
);
$hobbyCheckboxes = array(
    'cricket' => array(
        'name'    => 'hobbies',
        'id'      => 'hobbies_cricket',
        'value'   => 'Cricket',
        'checked' => in_array('Cricket'$selectedHobbies),
    ),
    'chess' => array(
        'name'    => 'hobbies',
        'id'      => 'hobbies_chess',
        'value'   => 'Chess',
        'checked' => in_array('Chess'$selectedHobbies),
    ),
    'kabbadi' => array(
        'name'    => 'hobbies',
        'id'      => 'hobbies_kabbadi',
        'value'   => 'Kabbadi',
        'checked' => in_array('Kabbadi'$selectedHobbies),
    ),
);

echo 
form_dropdown($statusDropdown$status$selectedStatus);

echo 
form_checkbox($hobbyCheckboxes['cricket']);
echo 
form_checkbox($hobbyCheckboxes['chess']);
echo 
form_checkbox($hobbyCheckboxes['kabbadi']);

/* OR, if you don't have 'value' and 'checked' in $hobbyCheckboxes */
echo form_checkbox($hobbyCheckboxes['cricket'], 'Cricket'in_array('Cricket'$selectedHobbies));
echo 
form_checkbox($hobbyCheckboxes['chess'], 'Chess'in_array('Chess'$selectedHobbies));
echo 
form_checkbox($hobbyCheckboxes['kabbadi'], 'Kabbadi'in_array('Kabbadi'$selectedHobbies)); 



RE: How to Keep maintain Radio button and chekbox Values After submit in form - Chandini - 05-07-2015

(05-07-2015, 07:22 AM)mwhitney Wrote: Ignoring the HTML, here's an example of how to do this, assuming that you pass the selected value of marital_status to the view as $marital_status and the values of the checked checkboxes in an array as $hobbies.


PHP Code:
<?php

$selectedStatus 
= isset($marital_status) ? $marital_status '-1';
$selectedHobbies = isset($hobbies) ? $hobbies : array();

$status = array(
    "-1"        => "Select One",
    "Married"   => "Married",
    "Unmarried" => "Unmarried",
    "divorced"  => "Divorced",
);
$statusDropdown = array(
    'name'  => 'marital_status',
    'id'    => 'marital_status',
    'class' => 'status',
);
$hobbyCheckboxes = array(
    'cricket' => array(
        'name'    => 'hobbies',
        'id'      => 'hobbies_cricket',
        'value'   => 'Cricket',
        'checked' => in_array('Cricket'$selectedHobbies),
    ),
    'chess' => array(
        'name'    => 'hobbies',
        'id'      => 'hobbies_chess',
        'value'   => 'Chess',
        'checked' => in_array('Chess'$selectedHobbies),
    ),
    'kabbadi' => array(
        'name'    => 'hobbies',
        'id'      => 'hobbies_kabbadi',
        'value'   => 'Kabbadi',
        'checked' => in_array('Kabbadi'$selectedHobbies),
    ),
);

echo 
form_dropdown($statusDropdown$status$selectedStatus);

echo 
form_checkbox($hobbyCheckboxes['cricket']);
echo 
form_checkbox($hobbyCheckboxes['chess']);
echo 
form_checkbox($hobbyCheckboxes['kabbadi']);

/* OR, if you don't have 'value' and 'checked' in $hobbyCheckboxes */
echo form_checkbox($hobbyCheckboxes['cricket'], 'Cricket'in_array('Cricket'$selectedHobbies));
echo 
form_checkbox($hobbyCheckboxes['chess'], 'Chess'in_array('Chess'$selectedHobbies));
echo 
form_checkbox($hobbyCheckboxes['kabbadi'], 'Kabbadi'in_array('Kabbadi'$selectedHobbies)); 

How to keep maintain the radio button values .... after submit ..? 
Selected And check box values Ok .. I have Problem With Radio buttons ...


RE: How to Keep maintain Radio button and chekbox Values After submit in form - mwhitney - 05-08-2015

As I mentioned previously the code for form_radio() just passes the arguments to form_checkbox, so you basically treat it the same as a checkbox. However, you won't have an array of values for a set of radio buttons, because only one in a group will be selected. So, instead of using
Code:
in_array($value, $selectedValues)
you would just use
Code:
$value === $selectedValue
for either the 'checked' attribute or the third parameter passed to form_radio().