CodeIgniter Forums
How to repopulate update form and radio inputs - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to repopulate update form and radio inputs (/showthread.php?tid=49759)



How to repopulate update form and radio inputs - El Forum - 03-02-2012

[eluser]benners[/eluser]
Can someone please point me in the right direction to re-populate the radio buttons on an update form. I think I need to utilize form validation but I've not been able to find enough information on this.

Controller: Quiz
Code:
function update_test($comp_img)
{
$quiz_data =  $this->quiz_model->get_quiz_data($comp_img);
$data['quiz'] = $quiz_data;
$data['main_content'] = 'quiz_edit';
$this->load->view('includes/template', $data);
}



View: quiz_edit.php
Code:
echo form_open('quiz/save');
$x = 1;
foreach ($quiz as $file_item):
$data = array(
    'name'        => $x.'[correct_answer]',
    'value'       => set_radio($x.'[correct_answer]', $file_item['correct_answer']),
  );
$data2 = array(
    'name'        => $x.'[correct_answer]',
    'value'       => set_radio($x.'[correct_answer]', $file_item['correct_answer']),
  );
echo 'A:'.form_radio($data);
echo 'B:'.form_radio($data2);
$x = $x + 1;
endforeach;



How to repopulate update form and radio inputs - El Forum - 03-02-2012

[eluser]Aken[/eluser]
set_radio() takes a third parameter of TRUE/FALSE to set whether or not the radio item is checked. You'll need to addd some logic that fetches the value of your radio, sees if it's the same value as that particular radio, then sets that one to TRUE.


How to repopulate update form and radio inputs - El Forum - 03-02-2012

[eluser]benners[/eluser]
I'm struggling to get the set_radio TRUE of FALSE check to work correctly. I've abandoned the array for the following which isn't working either.

Code:
echo form_open('quiz/save');
$x = 1;
foreach ($quiz as $file_item):
     echo "A:<input type='radio' name='".$x.'[correct_answer]'."' value='a' ".set_radio($x.'[correct_answer]', 'a', isChecked('a', $answer)). "/>";
     echo "B:<input type='radio' name='".$x.'[correct_answer]'."' value='b' ".set_radio($x.'[correct_answer]', 'b', isChecked('b', $answer)). "/>";

$x = $x + 1;
endforeach;

function isChecked($value, $master) {
if ($value == $master) {
  return 'TRUE';
} else {
  return 'FALSE';
}
}



How to repopulate update form and radio inputs - El Forum - 03-02-2012

[eluser]Aken[/eluser]
Sorry, my mistake - the form_radio() function has the third parameter, not set_radio(). set_radio() will return a string - either empty if the radio should be checked, or 'checked="checked"' if it is.


How to repopulate update form and radio inputs - El Forum - 03-02-2012

[eluser]porquero[/eluser]
I set forms this:

[controller]
Code:
$data = array(
'genre_selected' => 'male',
'genres' => array(
  'male',
  'female',
),
);

[view]
Code:
// Filed name
$field = 'genre';

// Set selected radio
$selected = array_fill_keys($genres, false);
$selected[$genre_selected] = true;

// Generate radios
foreach ($genres as $genre) {
    // Slug for id
    $radio_id = url_title($field . '_' . $genre, 'dash', true);
    // Label
    echo form_label($genre, $radio_id)
    // Show radio checking only selected. without if!!! :)
    . form_radio($field, $genre, $selected[$genre], 'id="' . $radio_id . '"');
}

Only you need change the $genre_selected variable and will checked correct radio.