Sandeep Kumar Sandeep Kumar - 2 months ago 8
HTML Question

How will I populate value in textarea and radio buttons

I have fetch data from database in $tableRe. Now I have to print values in textarea and also have to check the radio button.

Here is my code,

$sql = "select (address, gender) from stud table";
if($result=mysqli_query($conn,$sql)) {
while($row = mysqli_fetch_array($result)) {
$tableRe[]=$row;
}
}

<form>
Address : <br>
<textarea value="<?php echo $tableRe[0]['address']; ?>"></textarea><br>
Gender : <br>
<input type="radio" value="Male">Male
<input type="radio" value="Female">Female <br>
<input type="submit" value="Save">
</form>


Please help me regarding this. Thanks in advance.

Answer

You need to apply condition on checked HTML attribute.

Try this:

<form>
   Address : <br>
   <textarea><?php echo $tableRe[0]['address']; ?></textarea> <br/>
   Gender : <br>
   <input type="radio" value="Male" <?php echo $tableRe[0]['gender'] == 'Male' ? 'checked' : '';  ?> >Male
   <input type="radio" value="Female" <?php echo $tableRe[0]['gender'] == 'Female' ? 'checked' : '';  ?>>Female <br>
   <input type="submit" value="Save">
</form>