imran
imran

Reputation: 51

change value of a radio button based on database value in php

i have a radio button in my form

<input type="radio" id="radio-2" name="radio" checked  onclick="calculate()" >

how to set the radio button value based on database value like

if(db_val==2){
value=50
} else{
value=100
}

Upvotes: 1

Views: 583

Answers (1)

Emtiaz Zahid
Emtiaz Zahid

Reputation: 2835

Radio button value is depend on checked attribute

If you set the checked attribute with condition then its value will be set

Example:

<input type="radio" id="radio-2" name="radio" value="50" <?php echo ($db_val==2)?'checked':'' ?> onclick="calculate()" >
<input type="radio" id="radio-2" name="radio" value="100" <?php echo ($db_val!=2)?'checked':'' ?>  onclick="calculate()" >

Upvotes: 2

Related Questions