dpzyd8865 2015-02-25 23:35
浏览 61
已采纳

PHP - 从数据库中获取值并设置为HTML select标记的值

Hope someone can help me out. Developing a stock management system for a herb factory in PHP with a MySQL database. I'm currently developing a page where the user can edit the value of one of the benches that holds the herbs.

I've drawn in the benches details and they're in an array called $bench_data which contains variable herb, quantity, etc.

I have a html select tag where the user selects the herb being produced

Herb*:<br>
<select name='herb' ">
   <option value="default">-----</option>
   <option value="basil">Basil</option>
   <option value="coriander">Coriander</option>
   <option value="parsley">Parsley</option>
   <option value="mint">Mint</option>
   <option value="rosemary">Rosemary</option>
   <option value="thyme">Thyme</option>
</select>

What I want to do is have the herb value of the selected bench already in the select tag when the user enters the page.

I've managed to do it with quantity - 'the php in value'

Quanity<br>
<input type="text" name="quantity" value="<?php echo $bench_data['quantity']; ?>">

but I don't know how to do it when there are different options

Thanks in advance

  • 写回答

1条回答 默认 最新

  • dtdr57046 2015-02-25 23:40
    关注

    You use the "selected" attribute to tell the browser which option should be showing on page load. For example, on each option do it like this:

    <option value="basil" <?php if ($bench_data['herb']=='basil'){echo 'selected';}?>>
    Basil
    </option>
    

    Here's a helpful link

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题