简体   繁体   中英

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; ?>"

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"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM