I want to delete a line which contains something given by the $_POST['link']
It already works with the .csv-File. At this point, the code for the .txt-File does nohting.
My php code
//txt
$txt = file('../textFiles/websites.txt');
$fpTXT = fopen('../textFiles/websites.txt', 'w');
foreach ($txt as $lines) {
if (!str_contains($lines[2], strval($_POST['link']))) {
fwrite($fpTXT, $lines);
}
}
fclose($fpTXT);
the trigger with js
<!-- Delete-Function -->
<script>
async function deleteRequest(elem) {
$.ajax({
type: 'POST',
url: '/sites/deleting.php',
dataType: 'html',
data: {
'link': elem
}
});
location.reload();
};
</script>
</head>
<!-- Delete Icon -->
<?php $delBtn = "<td><button class='delete' onclick=\"deleteRequest(this.parentNode.parentNode.getElementsByTagName('td')[2].innerText)\"><i class='material-icons'></i></button></td>"; ?>
code for the csv (above the txt-code)
//CSV
$csv = array_map('str_getcsv', file('../textFiles/websites.csv'));
$fpCSV = fopen('../textFiles/websites.csv', 'w');
foreach ($csv as $fields) {
if (!str_contains($fields[2], strval($_POST['link']))) {
fputcsv($fpCSV, $fields);
}
}
fclose($fpCSV);
2 lines from the .txt-File