<?php
$cookie_name = "target";
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!"; return;
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
$gone = $_COOKIE[$cookie_name];
unlink($gone);
}
?>
<script>
function OK_delete (filename) {
let result = confirm('Are you sure you want to delete ' +filename +' ?');
if (!result) {return;}
if (result) { Cookies.set('target', filename, { expires: 1 });
test = Cookies.get('target'), alert (test); // WORKS OK!
// HERE I WANT TO ACCESS 'purge.php' AND RUN IT
// BUT I DON"T HAVE A CLUE HOW TO DO THAT!
// ANYBODY HAVE A SIMPLE, CORRECT SOLUTION ?
// FETCH
fetch("purge.php", { method: "POST", body: data })
.then(res => res.text())
.then(txt => console.log(txt))
.catch(err => console.error(err));
return false;
}
}
}
filename = 'myfile.txt';
OK_delete (filename);
</script>
Hi @verlager, you have an odd closing bracket in your JS, and the data you’re trying to send as the POST body in the fetch() call is not defined. After fixing those issues it’s working fine for me.
That said, why don’t you actually send the filename in the POST body, rather than setting it as a cookie?