skip to Main Content

The sweetalert2 script dont recognize by php using echo. I download the script file on github and when I put the sweetalert script to php, the script message doesn’t show on mine. But when i put the sweeetalert script to the script section, it works but its difficult cause when button is being clicked, the sweetalert script shown, which is i want to avoid. Here’s the code below:

Update function:

<?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 file:


<!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 -->

How can i show the sweetalert2 popup in my code

2

Answers


  1. I assume that you have already included the necessary js, such as:

    <script src="sweetalert2.all.min.js"></script>
    

    You then need to tell the system that the Swal.fire is a javascript function , so need to enclose it with <script>....</script>

    So change

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

    to

     if(mysqli_query($connection, $update_query)) {
                          echo "
                          <script>
                           Swal.fire({
                            title: 'Good job!',
                            text: 'You clicked the button!',
                            icon: 'success'
                          });
                          </script>
                          ";
                        }
    
    
    Login or Signup to reply.
  2. There are two parts to the code execution

    Server side:
    All PHP code is running on your server.
    Running the PHP echo only prints the text provided to the echo to the output. At the end PHP generates an HTML output that gets presented by your browser. PHP is executing on server side completely and it does not control execution of any scripts on the client side (your browser).

    Client Side:
    The Browser only runs anything that is present in HTML, CSS and Javascript. The alert and any any other javascript functions in the script tag are however executed by the browser when it encounters the script tag.

    I assume your PHP script here is triggered on a form submit and meant to fire a database query and the present the sweetalert2 alert on success instead of a regular javascript alert.
    What you really want to do is somehow trigger javascript execution after the PHP submit has got through successfully and subject to availability of some information from server side show the alert.

    Please go through this response to understand a better way to do this.

    Calling JavaScript function after PHP Submit

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search