Radio button check with BD data

Asked

Viewed 794 times

2

I’m having trouble taking the data from the database and passing via Session to my form, all the input data is coming correctly, I just can’t pass it as check for my radios... I tested in various 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

2 answers

0

Just add the checked

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

It’ll stay that way:

<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
  • The browser interprets the same result for the two types of syntax . <input type="radio" checked="<? php echo $checked1; ? >" /> OR <input type="radio" checked /> If there is any difference, please explain to me

  • is having the same problem as before... it is always checked only in legal even when the user is as a physical person

0

I checked that if you upload the page by F5 in the Firefox, it does not obey the stipulated rule, but leaves selected always the last (should cache). But if you reload with ENTER or CTRL+F5, it obeys the rule. Already in the Chrome, worked normal.

Below 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, do the manipulation by DOCUMENT.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.