Dial radiobutton in php [duplicate]

-1

How do I leave the radiobutton "checked" in PHP? I am having difficulty with this the name of the radiobutton is sex and I need to do a check in the database if the sex is 'M' it marks one of the radioButton sex otherwise it marks the other radioButton sex. I've done the SELECT and it returned the value of the other variables normally, I just want to know how to mark a radiobutton, my code is wrong? I already checked if the variable is receiving the sex value correctly and it is, the condition code is after the creation of the radioButton, I can not think of anything that is wrong, it can only be the way I'm dialing the radioButton.

 <label>Sexo:</label><input type="radio" name="sexo" value="M"/>
 <label>Masculino</label>

 <input type="radio" name="sexo" value="F"/>
 <label>Feminino</label>

 if($vsexo == 'M')
 {
  echo "<script type='text/javascript'>sexo[0].checked = true;</script>";
 }
 else
 {
  echo "<script type='text/javascript'>sexo[1].checked = true;</script>";
 }
    
asked by anonymous 27.11.2016 / 21:49

2 answers

0

Test like this:

<label><input value="M" type="radio" name="sexo" <?= $vsexo == 'M' ? 'checked="true"' : ''; ?>>Masculino</label>
<label><input value="F" type="radio" name="sexo" <?= $vsexo == 'F' ? 'checked="true"' : ''; ?>>Feminino</label>
    
28.11.2016 / 02:11
-2
$sexo = mysql_fetch_array(mysql_query("SELECT campo_sexo FROM tabeladedados"));
$sexo = ($sexo[0] || "M");
<?php
    if(strtolower($Sexo) == "M"):
?>
    <input type="radio" name="sexo" value="M" checked="checked" />
<?php
    else:
?>
    <input type="radio" name="sexo" value="M" />
<?php
    endif;
?>
    
27.11.2016 / 22:14