简体   繁体   English

回声确认PHP的JavaScript

[英]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: 是否有一些可能的方法来回显确认javascript以确认是否从数据库中删除值,或者我要这样做的是:

 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. 您可以这样进行:用户确认删除,然后Javascript向某个PHP页面启动ajax请求。 This page deletes the data and then answers in JSON. 此页面将删除数据,然后以JSON格式回答。 Javascript parses JSON and says if the operation was successful or not. Javascript解析JSON并指出操作是否成功。

What you are trying to do can be done using AJAX and PHP, for example: You can embed JavaScript inside PHP such as 您可以尝试使用AJAX和PHP完成操作,例如:您可以将JavaScript嵌入到PHP中,例如

   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. 但是,如果您使用AJAX,则无需将代码嵌入到PHP中,则可以将所有JavaScript代码写入一个单独的文件中,并使用AJAX发出PHP请求。

If you want a real example, let me know. 如果您想要一个真实的例子,请告诉我。

Confirm actions are meant to go inside javascript statements, they return true or false. 确认动作是要放入javascript语句中的,它们返回true或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. 基本上,这应该使用AJAX查询来处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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