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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With