• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Prepopulating radio input

Okay, I'm having a weird problem. Dunno where to go look for the solution though.

I'm building an edit form that has several inputs: textboxes, textareas, some checkboxes and a set of radio inputs.

Now I want to prepopulate the edit form, which works great for the text bits, but I can't get the radio buttons to prepopulate.

if( empty($this->validation->error_string)) {
    foreach ($fields as $field => $name) {
        $this->validation->$field = $data['tut'][$field];
That's the code I use in my controller, it simply puts the values from the DB in if validation hasn't run yet.

Now I've changed the set_radio thingie (which doesn't work with prepopulated data, as it only looks at $_POST and not at $this->validation) with my own if:

<input type="radio" name="rankID" value="<?=$rank['rankID']?>" id="rank-<?=$rank['rankID']?>" <?php if($select_rank == $rank['rankID']) echo 'selected="selected" '; ?>/>

Now this works, I can see the selected="selected" code in my source code, it changes correctly when you submit the form but validation fails etc.

HOWEVER: it doesn't get selected in the form in the browser itself. Has anybody got any idea what could be causing this?

If you see it changing setting the selected attribute works, or is there something i'm missing here?

The selected attribute is set. That's the whole weird thing. First I thought it was a FF bug, but it's happening in Chrome too...

Sorry for the crappy screenshot.

a radio button doesn't know the selected attribute. It has the checked attribute. sometimes i'm really clueless Smile

To make it easier on myself i made these functions
function form_selected($value1,$value2)
    return ($value1 == $value2)?' selected="selected"':'';

function form_checked($value1,$value2)
    return ($value1 == $value2)?' checked="checked"':'';

Oh god. Stupid me once again. And I even checked on the w3schools xhtml reference if it was selected and not checked. Must've gotten confused and thought I was right.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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