需要帮助从我的代码中删除文本文件和下拉列表中的行。我的选择下拉菜单是从文本文件中填充的。我想从下拉菜单中删除所选项目,并从表单提交中删除文本文件。这是我的尝试,但不起作用 - 提交时不会删除任何内容。
<?php
$filename = 'jvcities.txt';
$eachlines = file($filename, FILE_IGNORE_NEW_LINES);
?>
<form method="post" value="Get Selected Values">
<select name="usercitynya" required>
<option selected value="">Select Your City</option>
<?php foreach($eachlines as $lines){
echo "<option value='".$lines."'>$lines</option>";
}?>
$variable=$_REQUEST['usercitynya'];
</select><br />
<input name="submit2" type="submit" value="Submit" method="post" />
</form>
<?php
$DELETE = $variable;
$data = file("jvcities.txt");
$out = array();
foreach($data as $line) {
if(trim($line) != $DELETE) {
$out[] = $line;
}
}
$fp = fopen("jvcities.txt", "w+");
flock($fp, LOCK_EX);
foreach($out as $line) {
fwrite($fp, $line);
}
flock($fp, LOCK_UN);
fclose($fp);
?>