On submit remove item from dropdown list and text file

Need help to delete line from text file and dropdown list herewith my code. My select dropdown menu is populated from a text file. I want to delete selected item from dropdown menu and from text file on form submit. Here is my attempt but is not working - nothing gets deleted on submit.

<?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);  
 ?>
phpformsselectdelete-row

Answers

comments powered by Disqus