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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效