Search code examples
javascriptphpconfirmdelete-row

Echo confirm javascript with php


I was wondering. Is there some possible way to echo a confirm javascript to confirm if delete values from database or not what i'm trying to do is this:

 echo($strconfirm ="<script>javascript:confirm('Tem a certeza que pretende eliminar o registo?');</script>");

                if ($strconfirm == true)
                {

                    $query="DELETE FROM softwares WHERE Idsoft='".$id."'";
                    $result=mysqli_query($ligabd,$query);
                    if (!$result)
                    {
                        echo("<script>javascript:alert('Erro ao eliminar o produto!');window.location='produtos.php';</script>");
                    }
                    else
                    {
                        echo("<script>javascript:alert('Software eliminado com sucesso!');window.location='produtos.php';</script>");
                    }
                }
                else
                {
                    header("location:produtos.php");
                }

Solution

  • You can do it that way: A user confirms the delete and Javascript launches an ajax request to some PHP page. This page deletes the data and then answers in JSON. Javascript parses JSON and says if the operation was successful or not.