user1806136
user1806136

Reputation: 35

PHP - edit a Form Input Type Radio button

i have a form of type radio , when user fills it out and submit it i want to add the ability for user to edit and change his selected answers .. so when he clicks on (edit) he goes back to the form with his previous selected buttons .. i know how to get the data from the database , but how to make the previous selected buttons appears on the form ?

so one of the questions is like ..

<tr>
<th bgcolor='FF6600'> Clarity of speaking
(Could you hear the speaker properly and clearly?)<font size="4" > </font></th>
<td>  <input type="radio" name ="v3" value = "4"     onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v3" value = "3"    onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v3" value = "2"    onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v3" value = "1"    onclick="updateTotal();" /></td>    
</tr>

i have try , but it doesn't works out

checked="<?php echo $v3; ?>"

Upvotes: 0

Views: 1837

Answers (2)

Everton Yoshitani
Everton Yoshitani

Reputation: 946

Like this:

<tr>
<th bgcolor='FF6600'> Clarity of speaking
(Could you hear the speaker properly and clearly?)<font size="4" > </font></th>
<td>  <input type="radio" name ="v3" value = "4"<?php echo ($v3 == $value) ? ' checked="checked"' : null;?> onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v3" value = "3"<?php echo ($v3 == $value) ? ' checked="checked"' : null;?> onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v3" value = "2"<?php echo ($v3 == $value) ? ' checked="checked"' : null;?> onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v3" value = "1"<?php echo ($v3 == $value) ? ' checked="checked"' : null;?> onclick="updateTotal();" /></td>    
</tr>

I'm assuming that you print the radio options with a loop checking $value against $v3 to find the current selected option.

Upvotes: 0

Phil
Phil

Reputation: 164733

Depending on your DOCTYPE, the presence of the checked attribute is all that is required to show the radio button as checked.

What you need to do is wrap this attribute in a conditional, something like

<?php for ($i = 4; $i > 0; $i--) : ?>
<td>
<input type="radio" name="v3" value="<?= $i ?>"
       <?php if ($v3 == $i) ?>checked<?php endif ?>
        onclick="updateTotal()">
</td>           
<?php endfor ?>

This would suit an HTML Doctype. If you're using XHTML, change checked to checked="checked"

Upvotes: 1

Related Questions