Rescue radio input log - PHP

Asked

Viewed 19 times

0

Hello, I’m making an editing page, and in it I have an input type="radio", and I’m not getting the radio flagged according to the value in the database...

I’m trying to pull this way, but it doesn’t work, is always marked what has the value "F", even though M at the bank...

<?php
$sql = mysqli_query($con, "SELECT * FROM pacientes WHERE sus = '$sus' ");
while ($cont = mysqli_fetch_array($sql)) { ?>

    <div class="col">
    <label class="col-form-label">Sexo</label><p>
    <input type="radio" name="sexo" id="sexo" value="M" <?php if ($sexo = 'M') {echo ' checked ';} ?> />Masculino</label>
    <input type="radio" name="sexo" id="sexo" value="F" <?php if ($sexo = 'F') {echo ' checked ';} ?> />Feminino</label>
    </div>

<?php } ?>

Where am I going wrong?

  • You are assigning the value M to the variable $sex, to compare use two equal signs (==)

  • = defines a value ; == compare without typing ; === compares with typing

1 answer

0


I got it that way:

<input type="radio" name="sexo" value="M" <?php if($cont['sexo']=="M"){ echo "checked";}?> />Masculino</label>
<input type="radio" name="sexo" value="F" <?php if($cont['sexo']=="F"){ echo "checked";}?> />Feminino</label>

Browser other questions tagged

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