• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to repopulate update form and radio inputs

#1
[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;

#2
[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.

#3
[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';
}
}

#4
[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.

#5
[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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


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