• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
set_radio function

#1
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
Reply

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

#3
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: 6)

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: 5)

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: 19)

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

.php   EditarCaso.php (Size: 10.01 KB / Downloads: 5)
Reply

#4
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)?
Reply

#5
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.
What did you Try? What did you Get? What did you Expect?

Joined the CodeIgniter Community in 2009.          ( Skype: insitfx )
Reply

#6
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.
Reply

#7
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.
What did you Try? What did you Get? What did you Expect?

Joined the CodeIgniter Community in 2009.          ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.