0

<?php    
function delReponse($r_id) {
        $bdd = new PDO($arrayConnection);

        $reponse = $bdd->prepare('DELETE FROM `topics_reponses` WHERE `id`=?');
        $reponse->execute(array($r_id));
    }
?>

    <script>
        var res = window.confirm('Are you sure you want to delete this message?');
        if (res) {
            document.write("<?php delReponse($r_id)?>");
            alert("Le message a bien été supprimé.");
        } else
            alert("Le message n'a PAS été supprimé.");
        window.close();
    </script>

Even if you click on "cancel", the php function which deletes an entry in the DB is executed.

  • PHP runs before the page is sent to the browser, javascript after. If you want to delete via a button, you'll have to have it call an ajax function to send a request back to the server. – aynber Aug 07 '20 at 13:27
  • That's not how php works, it gets processed before page load therefore calling the function in your javascript will always run every time you refresh the page when it processes the javascript. You need to create an ajax call and run the delete code on the server – justrusty Aug 07 '20 at 13:27
  • php runs on the server, before any javascript even gets to the browser. whatever php code you have on your page will have already executed before your browser ever even sees any javascript. you can't control php code directly from javascript like this. the only way to this is to use ajax to make a call to a php script. – I wrestled a bear once. Aug 07 '20 at 13:28

0 Answers0