sweetalert2 脚本在得到回显时不会在 php 中显示弹出消息

问题描述 投票:0回答:1

sweetalert2 脚本无法被 php 使用 echo 识别。我在 github 上下载了脚本文件,当我将 sweetalert 脚本放入 php 时,脚本消息不会显示在我的上。但是,当我将 sweetalert 脚本放入脚本部分时,它可以工作,但它很困难,因为当单击按钮时,会显示 sweetalert 脚本,这是我想避免的。代码如下:

更新功能:

<?php
// UPDATE FUNCTION

include '../connection.php';

if(isset($_POST['update'])) {
    
    $assetsId = $_POST['assetsId']; // RETRIEVE THE ID  OF THE assetsId TO BE UPDATED VIA HIDDEN FIELD IN HTML
    $assetsName = $_POST['assetsName'];
    $assetsDesc = $_POST['assetsDesc'];
    $assetsQuantity = $_POST['assetsQuantity'];
    $assetsLocation = $_POST['assetsLocation'];
    $assetsStatus = $_POST['assetsStatus'];
    $assetsUsage = $_POST['assetsUsage'];

    //QUERY OF UPDATE
    $update_query = "UPDATE Admin_add_assets SET 
                    assetsName = '$assetsName', 
                    assetsDesc = '$assetsDesc', 
                    assetsQuantity = '$assetsQuantity', 
                    assetsLocation = '$assetsLocation', 
                    assetsStatus = '$assetsStatus', 
                    assetsUsage = '$assetsUsage' 
                    WHERE assetsId = $assetsId";

                    if(mysqli_query($connection, $update_query)) {
                      echo "
                      Swal.fire({
                        title: 'Good job!',
                        text: 'You clicked the button!',
                        icon: 'success'
                      });
                      ";
                    } else {
                      echo "<script>alert('Error deleting record: " . mysqli_error($connection) . "');</script>";
                    }
                    
}
?>

html 文件:


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="../js/sweetalert2.min.js"></script>
  <link rel="stylesheet" href="style.css">
  <link rel="stylesheet" href="../css/bootstrap.min.css">
  <link rel="stylesheet" href="../css/sweetalert2.min.css">
  <title>DASHBOARD (ADMIN)</title>
</head>

<!-- and so fort -->

如何在我的代码中显示 sweetalert2 弹出窗口

php html sweetalert2
1个回答
0
投票

您需要告诉系统Swal.fire是一个javascript函数,因此需要用

<script>....</script>

将其括起来

所以改变

 if(mysqli_query($connection, $update_query)) {
                      echo "
                      Swal.fire({
                        title: 'Good job!',
                        text: 'You clicked the button!',
                        icon: 'success'
                      });
                      ";
                    }

 if(mysqli_query($connection, $update_query)) {
                      echo "
                      <script>
                       Swal.fire({
                        title: 'Good job!',
                        text: 'You clicked the button!',
                        icon: 'success'
                      });
                      </script>
                      ";
                    }

© www.soinside.com 2019 - 2024. All rights reserved.