Home > database >  Setting radio buttons from MySQL database
Setting radio buttons from MySQL database

Time:02-26

I'm trying to adopt this code taken from w3school.com but cannot see how I can preset the buttons by PHP based on the value stored in a Boolean MySQL column.

<form>
  What color do you prefer?<br>
  <input type="radio" name="colors" id="yes" value="1" >Yes<br>
  <input type="radio" name="colors" id="no" value="0" >No
</form>

<button onclick="check()">Check "Yes"</button>
<button onclick="uncheck()">Uncheck "Yes"</button>

<script>
function check() {
  document.getElementById("yes").checked = true;
}
function uncheck() {
  document.getElementById("yes").checked = false;
}
</script>

CodePudding user response:

You can use checked attribute with echo. I don't know how you get your data but for exemple :

<form>
  What color do you prefer?<br>
  <input type="radio" name="colors" id="yes" value="1" >Yes<br>
  <input type="radio" name="colors" id="no" value="0" <?php echo $variableOnMyDatabase ? "checked": "" ?> >No
</form>
  • Related