Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populating a dropdown list with values from a text file

Tags:

html

php

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>
like image 749
gram95 Avatar asked Jan 03 '23 12:01

gram95


1 Answers

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>
like image 191
Anant Kumar Singh Avatar answered Jan 06 '23 02:01

Anant Kumar Singh