Pramit
Pramit

Reputation: 1411

Populate HTML radio button based on user choice submitted in Database

One quick question. I am creating a web page using PHP, HTML and my requirement is to select a radio button by depending on choice(say 'Yes' or "No', yes selected by default). User selects 'No' and submits a form. I enter the choice in a database. When the site is re-visited, How do i make the selection based on the value retrieved from the database? I am able to get the values selected using PHP to connect to datatbase.

Thanks for the help. Let me know if more info is required, Below is the current code snippet.

<input type="radio" class="form-radio" id="Areyou3" name="Areyou3" value="1" />
 <label for="input_40_0"> Yes </label></span><span class="clearfix"></span>
  <span class="form-radio-item" style="clear:left;">
   <input type="radio" class="form-radio" id="Areyou3" name="Areyou3" value="0" />
   <label for="input_40_1"> No </label></span><span class="clearfix"></span>

Upvotes: 2

Views: 709

Answers (2)

Duke
Duke

Reputation: 37070

Try this

<?php 
if($valFromDB=='Yes')
$checked = 'checked';
else
$checked = '';
?>
<input type="radio" class="form-radio" id="Areyou3" name="Areyou3" value="1"  <?php echo $checked?>/>
<label for="input_40_0"> Yes </label></span><span class="clearfix"></span>
<span class="form-radio-item" style="clear:left;">
<input type="radio" class="form-radio" id="Areyou3" name="Areyou3" value="0" <?php echo $checked?>/>
<label for="input_40_1"> No </label></span><span class="clearfix"></span>

Upvotes: 0

Bananaapple
Bananaapple

Reputation: 3114

Something along these lines:

<input type="radio" name="Areyou3" value="yes"<?PHP ('yes' == $valueInDatabase)? ' checked="checked"' : '' ?> />
<input type="radio" name="Areyou3" value="no"<?PHP ('no' == $valueInDatabase)? ' checked="checked"' : '' ?> />

Upvotes: 1

Related Questions