如何使用PHP修改表单输出值的字体大小?
实现步骤
1. 调整表单单选按钮配置
首先确保你的单选按钮组设置统一的name属性(示例为font_size),PHP才能正常接收选中值,原有给单选按钮分配的ID无需修改,不影响你已经写好的CSS规则生效,表单结构参考:
<form method="post" action=""> <label><input type="radio" name="font_size" id="size15" value="15" <?php if(isset($_POST['font_size']) && $_POST['font_size'] == '15') echo 'checked';?>> 15px</label> <label><input type="radio" name="font_size" id="size20" value="20" <?php if(isset($_POST['font_size']) && $_POST['font_size'] == '20') echo 'checked';?>> 20px</label> <label><input type="radio" name="font_size" id="size24" value="24" <?php if(isset($_POST['font_size']) && $_POST['font_size'] == '24') echo 'checked';?>> 24px</label> <label><input type="radio" name="font_size" id="size30" value="30" <?php if(isset($_POST['font_size']) && $_POST['font_size'] == '30') echo 'checked';?>> 30px</label> <button type="submit" name="submit">提交设置</button> </form>
2. PHP侧参数接收与合法性校验
在页面头部、或者底部课程名称渲染前的位置加入PHP逻辑,通过白名单过滤非法输入,避免恶意传参导致的样式异常或安全问题:
<?php // 定义合法字号白名单 $allow_sizes = ['15', '20', '24', '30']; // 设置默认字号 $current_size = '15'; // 校验提交参数 if (isset($_POST['submit']) && isset($_POST['font_size']) && in_array($_POST['font_size'], $allow_sizes)) { $current_size = $_POST['font_size']; } ?>
3. 给底部课程名称绑定字号样式
根据你已写好的CSS规则二选一即可:
- 若你预设了和单选ID对应的类名规则(比如
.size15{font-size:15px}),使用类名绑定:
<div class="size<?php echo $current_size;?>"> 课程名称 </div>
- 若不想依赖预设类,直接绑定行内样式即可:
<div style="font-size:<?php echo $current_size;?>px"> 课程名称 </div>
补充说明
如果你的表单是提交到其他页面,需要把接收到的$current_size通过session、URL参数等方式传递到目标页面,再渲染课程名称即可生效。
内容的提问来源于stack exchange,提问作者qlows
相关产品推荐
相关产品推荐

