Always prefix asset urls with /
<script src="/assets/package/dist/sweetalert2.all.js"></script>
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Please i need help, i don't know why sweetAlert is not working despite doing everything correctly, (if i write my php code in the same place with the form it works fine, but i can't write my code that way). Here is my code,
<?php
session_start();
include("database/userController.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="assets/package/dist/sweetalert2.all.js"></script>
<link rel="stylesheet" href="assets/package/dist/sweetalert2.min.css">
</head>
<body>
<div>
<form action="" method="post">
<button type="submit" name="int_trans">submit</button>
</form>
</div>
</body>
</html>
my php file
<?php
if(isset($_POST['int_trans'])) {
echo "<script>
Swal.fire(
'Good job!',
'You clicked the button!',
'success'
)
</script>";
}
?>
Just a simple code that refused to run, the error i get in my console is
@Megatech ok first off. It is echoing things so that should inside your body tag on the page. Currently you are outputting it before html is even being rendered. Secondly, perhaps you should have some function you could call?
Please or to participate in this conversation.