最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Echo confirm javascript with php - Stack Overflow

programmeradmin4浏览0评论

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  sucesso!');window.location='produtos.php';</script>");
                    }
                }
                else
                {
                    header("location:produtos.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  sucesso!');window.location='produtos.php';</script>");
                    }
                }
                else
                {
                    header("location:produtos.php");
                }
Share Improve this question asked May 16, 2014 at 19:03 Miguel MachadoMiguel Machado 2982 gold badges4 silver badges16 bronze badges 2
  • 1 PHP and JavaScript do not interact...What you are trying to do is impossible with that pattern. – epascarello Commented May 16, 2014 at 19:05
  • 1 JS alert has to be triggered by a JS event. You can't just inject alert to website and hope it works. In other words: no, not possible. My advice: confirm with a modal window. – gh0st Commented May 16, 2014 at 19:07
Add a ment  | 

3 Answers 3

Reset to default 4

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.

What you are trying to do can be done using AJAX and PHP, for example: You can embed JavaScript inside PHP such as

   echo "<script>var confirm = confirm("Are you sure?");
          if(confirm){ 
             //Call PHP with AJAX here 
           }
          </script>";

But if you use AJAX you don't need to embed your code inside PHP, you can write all your JavaScript code in a separate file and make PHP requests using AJAX.

If you want a real example, let me know.

Confirm actions are meant to go inside javascript statements, they return true or false. Like so:

if(confirm("Save file?")){
        // Do somethign if use clicks OK
    } else {
        // Do somethign if use clicks Cancel
    }

So, your code should be something like:

<?php
 echo("<script>if(confirm('Tem a certeza que pretende eliminar o registo?')){
    // Use AJAX here to send Query to a PHP file
 } else {
    window.location='produtos.php';
 };</script>");
?>

Basically, this should be handled with an AJAX query.

发布评论

评论列表(0)

  1. 暂无评论