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

gra*_*m95 4 html php

我正在尝试使用文本文件中的值填充下拉列表。我有以下代码,但是,下拉列表中没有任何内容。谁能帮我?

这是我的 jquery 和 HTML 下拉列表的代码:

<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>
Run Code Online (Sandbox Code Playgroud)

Ana*_*Die 5

1.您当前的代码页必须是一个.php页面。(页面的扩展名必须是.php

2.更改代码如下:-

<?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>
Run Code Online (Sandbox Code Playgroud)