2019-04-18 11:23



I have a code to insert data to MySQL which is working well, I want to replace the current java script alert to sweetalert2. I went through all forums but couldn't find anything. can anyone help me? Thank you.

//include "dbcon.php";

$conn = new PDO('mysql:host=localhost; dbname=fleet','root', 'root'); 

$approver_email = $_POST['approver_email'];
//employee_name = $_POST['employee_name'];  
$employee_name = $_POST['employee_name'];
$approver_name = $_POST['approver_name'];
$employee_title = $_POST['employee_title'];
//$no_of_passengers = '';
//$name_of_passengers = '';

$no_of_passengers = $_POST['no_of_passengers'];
$name_of_passengers = $_POST['name_of_passengers'];

//$no_of_passengers2 = $_POST['no_of_passengers2'];
//$name_of_passengers2 = $_POST['name_of_passengers2'];

//$no_of_passengers2 = '';
//$name_of_passengers2 = '';


//include "mail.php";

// $no_of_passengers2 = $_POST['no_of_passengers2']; // $name_of_passengers2 = $_POST['name_of_passengers2'];

if (isset($_POST['submit'])) {

       // $sql2='';

        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $sql = "INSERT INTO fms_booking_request (requested_by, approver, approving_dept, no_of_passengers, name_of_passengers, departure_place, arrival_place, reason, request_timestamp,departure_date, return_date, departure_time, return_time)
        VALUES ('$employee', '$approver', '$department', '$no_of_passengers', '$name_of_passengers', '$depart_place', '$arrival_place', '$reason','$request_timestamp','$depart_date', '$return_date','$depart_time','$return_time')";


        /*echo "<br><br>You have successfully booked for a vehicle.<br>
              An emial is being sent to approver, Please wait for 5 seconds.<br>
        include "mail.php";

        echo "<script>alert('Successfully booked for a vehicle, email has been sent to approver'); window.location='vehicle_booking_form.php'</script>";



    echo "<script>alert('Please try again!'); window.location='vehicle_booking_form.php'</script>";



I want to use this sweetalert

Swal.fire({ position: 'top-end', type: 'success', title: 'Your work has been saved', showConfirmButton: false, timer: 1500 })

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • douyou7878 douyou7878 2年前

    For using it in php you can use the following code. First, you have to include the sweetalert library as I have included. Then you can use the sweet alert as I have used in the echo. Sweetalert doesn't load until the dom elements get loaded. So to tackle this situation I have used jquery's $(document).ready() function.

    NOTE: Don't forget to include jquery library at the top as well

      <script src="jquery-3.3.1.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>
    echo '
    <script type="text/javascript">
        position: "top-end",
        type: "success",
        title: "Your work has been saved",
        showConfirmButton: false,
        timer: 1500
    点赞 评论 复制链接分享
  • douguio0185 douguio0185 2年前

    You need to download and include sweetalert js library or use an online version.

    <!-- SweetAlert2 -->
    <script type="text/javascript" src='../files/bower_components/sweetalert/js/sweetalert2.all.min.js'> </script>
    <!-- SweetAlert2 -->
    <link rel="stylesheet" href='../files/bower_components/sweetalert/css/sweetalert2.min.css' media="screen" />

    Then in the code you replace all js alert to swal.

     position: 'top-end',
     type: 'success',
     title: 'Your work has been saved',
     showConfirmButton: false,
     timer: 1500

    All other options for sweetalert2 you can find here https://sweetalert2.github.io/

    点赞 评论 复制链接分享