duangai9678 2011-03-17 11:00
浏览 32
已采纳

使用PHP从MYSQL填充的动态html / javascript下拉列表

I think I must be missing something very simple here (and apologies if I am).

I have a html doc with javescript in it, and in that html doc, I want to create a dropdown list whose values are automtically populated from the result of a mysql query.

I'm guessing that I need to run a php script to get the info from the database, but then how to get that info into the dropdown is a mystery to me.

Please could someone point me in the wright direction - perhaps an example where someone has already done this.

I can't just write the dropdown in PHP as it would mean going away from the html page that I have everything else on.

Many thanks,

Rob.

  • 写回答

3条回答 默认 最新

  • doutuoji8418 2011-03-17 11:13
    关注

    This a very broad question, so I can give you a broad answer. Simple guidelines.

    You have some options:

    Put PHP in the HTML

    • rename your .html to .php or change your webserver settings (through .htaccess probably if you use Apache) to let php process .html files
    • put something like this into the HTML code at the appropriate place:

    xy.php/html

    <select name="fos">
    <?php
      //php code to get data from mysql
      //foreach/while to iterate through elements
        echo '<option value="'.$key.'">'.$value.'</option>';
      //end of foreach/while
    ?>
    </select>
    

    Use Ajax to load data

    • leave the HTML as it is now
    • write a PHP script to output the dropdown
    • write Javascript to get the output of the PHP script with Ajax, then insert it to the DOM in the appropriate place
    • the downside is that people without JS won't see the dropdown

    EDIT: or Take the approach that Haza suggested

    Whichever you choose, I've given you the keywords to search for or ask further questions on

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私