简体   繁体   中英

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");
                }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM