Check on Radio button with DB data

1

I'm having problems getting the data from the bank and passing via session to my form, all the input data is coming correctly, I just can not pass it as check to my radios ... I tested it in several ways, one of them was:

<div class='row'>
        <div class='col-lg-9'>
            <div class='radio'>
                <label for='fisica'>
                    <input type='radio' tabindex="1" class="tipo_pessoa" checked="checked" name='tipo_pessoa' id='fisica' value='fisica' />Pessoa Física</label>
            </div>
            <div class='radio'>
                <label for='juridica'>
                    <input type='radio' tabindex="2" class="tipo_pessoa" name='tipo_pessoa' id='juridica' value='juridica' />Pessoa Jurídica</label>
            </div>
        </div>
    </div>

<?
$value=$row['tipo_pessoa'];
 
$checked1=($value=="fisica")?'checked':'';
$checked2=($value=="juridica")?'checked':'';
?>
 
<input type="radio" name="tipo_pessoa" value="fisica" <?php echo $checked1; ?> />
Fisica

<input type="radio" name="tipo_pessoa" value="juridica" <?php echo $checked2; ?> />
Juridica
    
asked by anonymous 06.06.2016 / 16:16

2 answers

0

Just add checked

checked="<?php echo $checked1; ?>"

It will look like this:

<input type="radio" name="tipo_pessoa" value="fisica" checked="<?php echo $checked1; ?>" />
Fisica

<input type="radio" name="tipo_pessoa" value="juridica" checked="<?php echo $checked2; ?>" />
Juridica
    
06.06.2016 / 17:14
0

I've found that if you load the page with F5 in Firefox , it does not obey the stipulated rule, but always leaves the last selected one (must cache). But if you reload with ENTER or CTRL + F5 , it obeys the rule. Already in Chrome , it worked normal. Here is an example:

 <?php
        $row['tipo_pessoa'] = 'juridica';
        ?>
        <div class='row'>
            <div class='col-lg-9'>
                <div class='radio'>
                    <label for='fisica'>
                        <input type="radio" name="tipo_pessoa" value="fisica" <?= ($row['tipo_pessoa'] == 'fisica') ? 'checked' : '' ?> />Fisica
                    </label>
                </div>
                <div class='radio'>
                    <label for='juridica'>
                        <input type="radio" name="tipo_pessoa" value="juridica" <?= ($row['tipo_pessoa'] == 'juridica') ? 'checked' : ''  ?> />Juridica
                    </label>
                </div>
            </div>
        </div>

My suggestion: If the "problem" persists, handle it DOCUMENT .

    
06.06.2016 / 21:38