douao1579 2017-09-04 06:15
浏览 106
已采纳

使用文本文件中的值填充下拉列表

I am trying to populate a dropdown list with values from a text file. I have the following code, however, I am not getting any thing within the dropdown list. Can anyone help me?

Here's my code for the jquery and HTML dropdown list:

<script>
$filename = 'pytxt.txt';
$eachlines = file($filename, FILE_IGNORE_NEW_LINES);
echo '<select name="value" id="value">';
foreach($eachlines as $lines){
echo "<option>($lines)</option>";
}
echo '</select>';

</script>
</head>
<body>
    <div id="page-wrap">
        <h1>Pulls from text files</h1>
        <select id="value">
            <option selected value="base">Please Select</option>
            <option value="1"></option>
            <option value="2"></option>
        </select>

    </div>
</body>
  • 写回答

3条回答 默认 最新

  • doujian0265 2017-09-04 06:22
    关注

    1.your current code page must be a .php page.(extension of the page need to be .php)

    2.change code like below:-

    <?php
    //remove <script></script> and add php start and close tag
    //comment these two lines when code started working fine
    error_reporting(E_ALL);
    ini_set('display_errors',1);
    
    $filename = 'pytxt.txt';
    $eachlines = file($filename, FILE_IGNORE_NEW_LINES);
    
    ?>
    <body>
        <div id="page-wrap">
            <h1>Pulls from text files</h1>
            <select id="value">
                <option selected value="base">Please Select</option>
               <?php foreach($eachlines as $lines){ //add php code here
                    echo "<option value='".$lines."'>$lines</option>";
                }?>
            </select>
        </div>
    </body>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)