duangang4001 2019-04-18 11:23
浏览 220
已采纳

如何在php中使用sweetalert2

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.

<?php
//session_start();
//include "dbcon.php";
error_reporting(0);

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

$department=$_POST['department'];
$employee=$_POST['employee'];
$approver=$_POST['approver']; 
$depart_date=$_POST['depart_date'];
$return_date=$_POST['return_date'];
$depart_time=$_POST['depart_time'];
$return_time=$_POST['return_time'];
$depart_place=$_POST['depart_place'];
$arrival_place=$_POST['arrival_place'];
$reason=$_POST['reason'];
$request_timestamp=$_POST['request_timestamp'];    
$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 = '';

$sql2='';

//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')";

        $conn->exec($sql);
        //$conn->exec($sql2);

        /*echo "<br><br>You have successfully booked for a vehicle.<br>
              An emial is being sent to approver, Please wait for 5 seconds.<br>
              *<br>
              **<br>
              ***<br>
              ****<br>
              *****<br>
              ******<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>";

} 

else{

    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 })

  • 写回答

2条回答 默认 最新

  • douyou7878 2019-04-18 11:52
    关注

    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

    <head>
      <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>
    </head>
    
    <?php
    
    echo '
    <script type="text/javascript">
    
    $(document).ready(function(){
    
      swal({
        position: "top-end",
        type: "success",
        title: "Your work has been saved",
        showConfirmButton: false,
        timer: 1500
      })
    });
    
    </script>
    ';
     ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器