Welcome Guest, Not a member yet? Register   Sign In
Form validation with radio buttons when updating form
#1

[eluser]sdotsen[/eluser]
I have a form that pulls data from a database.
Code works fine if it's an input box but I can't get the latest $_POST data for radio buttons.

This works for input text box. I get default values pulled from the DB on first load, and I can get the new input (if any) from the user modifies the input box on a failed validation.

Code:
<?php echo form_input('email',set_value('email', $email)); ?>

Here's the code for one of my radio button. It works when I'm pulling data from the DB, but if the form refreshes due to a failed validation, I'm not sure how I can show what the user selected.

Code:
<input type="radio" name="gender" value="male" <?php if($gender == "male") echo "checked";  ?> />Male

I played around with set_radio but i ran into the same issue.
#2

[eluser]gigas10[/eluser]
Code:
<input type="radio" name="gender" value="male" <?php if(set_value('gender', $gender) == "male") echo "checked";  ?> />Male

Try that
#3

[eluser]Selene[/eluser]
I still cannot get my saved radio box information to redisplay on the form. Have you been able to get this to work?
#4

[eluser]LuckyFella73[/eluser]
I had the same problem and came to this solution:
Code:
// Controller part where you get data from database:
if ($query->num_rows() > 0)
{
    foreach ($query->result() as $row)
    {
        $data['gender'] = $row->gender;
    }

    if ( $data['gender'] == 'male')
    {
        $data['genderflag_male'] = TRUE;
        $data['genderflag_female'] = '';
    }
    else
    {
        $data['genderflag_male'] = '';
        $data['genderflag_female'] = TRUE;
    }
}

// view part radio:
<input class="radio" type="radio" name="gender" value="male" <?php echo set_radio('gender', 'male', $genderflag_male); ?> />
<input class="radio" type="radio" name="gender" value="female" <?php echo set_radio('gender', 'female', $genderflag_female); ?> />

If somebody knows a better/cleaner/more portable way to do that I would be happy to read here


EDIT:
After submitting the form I then have this in case the validation didn't went trough:
Code:
$data['gender'] = $this->input->post('gender');
if ( $data['gender'] == 'male')
{
    $data['genderflag_male'] = TRUE;
    $data['genderflag_female'] = '';
}
else
{
    $data['genderflag_male'] = '';
    $data['genderflag_female'] = TRUE;
}
#5

[eluser]Selene[/eluser]
Thank you LuckyFella73

I will try you solution when I get home from this job to work on my other job.




Theme © iAndrew 2016 - Forum software by © MyBB