douao1579 2017-09-03 22: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-03 22: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部