CodeIgniter Forums
set_radio function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: Issues (https://forum.codeigniter.com/forumdisplay.php?fid=19)
+--- Thread: set_radio function (/showthread.php?tid=70049)



set_radio function - danielcr_2018 - 02-12-2018

Hi, I'm Daniel, and I've been using Code Igniter for two months. I'm really thankful for the work on this framework.

I'm developing a little application and found a strange behavior in set_radio function, from the Form Helper. I would like to ask if this behavior is correct or it's a bug.
I have an update form. I have a list of links that represent people, so the user clicks a link and gets the form populated with person's data. With this way, passing parameters through GET method, everything works fine. All fields are populated correctly. 
Today I figure a security problem with this approach so I changed things a little. Instead of a list of links I created a form with options, using radio buttons, so the user selects an option and his choice is sent to the application through POST. Then, the form is displayed and populated with the respective data. With this approach, all fields are populated except the radio button fields.

With gratitude,
Daniel


RE: set_radio function - Wouter60 - 02-12-2018

Please, share your controller (or just the function in your controller that prepares the form) and your view.


RE: set_radio function - danielcr_2018 - 02-13-2018

OK, the first file is listar_casos.php. This is the view showing the people, where the user choose a person to edit it's data. This file sends sends person's ID to the controller as a query string.

.php   listar_casos.php (Size: 1.35 KB / Downloads: 36)

The second file is listar_casos_post.php. This is the same as the previous file, but instead it sends person's ID through POST.

.php   listar_casos_post.php (Size: 1.49 KB / Downloads: 37)

This is Seremi.php, the complete controller. The method which prepares the form is called editar_caso.

.php   Seremi.php (Size: 11.61 KB / Downloads: 156)

Finally EditarCaso.php. This is the view showing the update form.

.php   EditarCaso.php (Size: 10.01 KB / Downloads: 39)


RE: set_radio function - Wouter60 - 02-13-2018

In your EditarCaso view, replace lines 114-118 with:
PHP Code:
<td>&nbsp
    <?= 
form_radio('brote','si',set_radio('brote','si',$caso[0]->TipoEvento == 'si'));?>SI&nbsp;&nbsp;
    <?= form_radio('brote','no',set_radio('brote','no',$caso[0]->TipoEvento == 'no'));?>NO
</td> 

Is the TipoEvento property a varchar field that can hold the value 'si' or 'no', or is it a boolean field (0 or 1)?


RE: set_radio function - InsiteFX - 02-14-2018

A peculiarity of HTML Checkboxes and Radio Buttons:

If they are not ticked (checked), they have no value at all, so nothing is returned!

If you try your code without checking if the checkboxes or radio buttons are set,
then you'll have to deal with a lot of "Undefined" errors.

PHP Code:
// Check Boxes
$value = (isset($checkBox))
 
               'checked'
 
               'unchecked';

// Radio Buttons
$value = (isset($radioButton))
 
               'checked'
 
               'unchecked'

Assign the $value of the $checkBox or $radioButton that you are checking for from your form.


RE: set_radio function - danielcr_2018 - 02-14-2018

I changed the code to use form_radio, but it didn't work. So I looked into the set_radio declaration, in system/helpers/form_helper.php, and made a few changes, and now it's working.

I'm taking your recommendation InsiteFX.

Thank you for your help guys.


RE: set_radio function - InsiteFX - 02-15-2018

If a checkbox or radio button is not checked then they return nothing
an empty value which will generate errors or give the wrong result.