简体   繁体   English

显示单选按钮选择php?

[英]Displaying radio button choice php?

My code works fine after the form fill out all values are print accept my gender value.在表格填写完所有值后,我的代码工作正常,打印接受我的性别值。

Here in the code below is an example imagine firstName,lastName.... the $row[dob] value prints out successfully.下面的代码是一个例子,假设 firstName,lastName.... $row[dob]值成功打印出来。

But the option for the gender is not printing is there some array function i need to use for this to have the option selected bu the user print back on the screen when only display the details of the user (similar to a sticky form)?但是性别的选项不打印是否有一些数组函数我需要使用它来选择选项但用户在仅显示用户的详细信息时在屏幕上打印回(类似于粘性表格)?

    <li><label for='dateOfBirth'>DOB:</label>
    <input type='date' name='dateOfBirth' value = $row[dob] min='1900-01-01' max= '2015-01-01'/></li>

    <li>
        <input type='radio' name='gender' value=$row[gender]'>
        <label for='male'>Male</label><br>
        <input type='radio' name='gender' value='female'>
        <label for='female'>Female</label><br>
    </li>

you need to use the checked attribute你需要使用checked属性

if the value that comes from $row['gender'] is male check the male radio otherwise check the female如果来自$row['gender']男性检查男性收音机,否则检查女性

like this像这样

<li>
     <input type='radio' name='gender' <?= $row[gender] == 'male'? 'checked':'' ?> value='male'>
     <label for='male'>Male</label><br>
     <input type='radio' name='gender' <?= $row[gender] == 'female'? 'checked':'' ?> value='female'>
     <label for='female'>Female</label><br>
</li>

You shouldn't set the value of the radio inputs, instead determine if the value is checked.您不应该设置无线电输入的值,而是确定是否选中了该值。

Example:例子:

<li>
    <input type='radio' name='gender' value='male' <?= $row['gender'] === 'male' ? 'checked' : null ?>>
    <label for='male'>Male</label><br>
    <input type='radio' name='gender' value='female' <?= $row['gender'] === 'female' ? 'checked' : null ?>>
    <label for='female'>Female</label><br>
</li>

NOTE: Your condition <?= $row['gender'] === 'female' ? 'checked' : null ?>注意:您的条件<?= $row['gender'] === 'female' ? 'checked' : null ?> <?= $row['gender'] === 'female' ? 'checked' : null ?> may vary depending on how you template your HTML. <?= $row['gender'] === 'female' ? 'checked' : null ?>可能会因您的 HTML 模板方式而异。

Sandbox 沙盒

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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