在PHP中保存数据后的Sweet Alert

时间:2019-04-04 08:03:01

标签: php sweetalert

保存数据后,我想显示Sweetalert消息。但是我遇到了一些问题。请在我的代码中对其进行更正。我使用PHP作为我的后端语言,并使用MYSQL作为我的数据库。这是我的USER_SAVE.php。我已经在搜索相同的场景,我已经尝试过对我不起作用的代码,可能是该代码已被弃用或使用了其他版本的sweetalert或其他版本。

 <html>
  <link rel='stylesheet' href='https://cdn.rawgit.com/t4t5/sweetalert/v0.2.0/lib/sweet-alert.css'>


<?php session_start();
if(empty($_SESSION['id'])):
    header('Location:../index');
endif;

include('../dist/includes/dbcon.php');
$rec= mysqli_real_escape_string($con,$_POST['rec']);
$bdo= mysqli_real_escape_string($con,$_POST['bdo']);
$can_name= mysqli_real_escape_string($con,$_POST['can_name']);
$po_ti= mysqli_real_escape_string($con,$_POST['po_ti']);
$client= mysqli_real_escape_string($con,$_POST['client']);
$rdr= mysqli_real_escape_string($con,$_POST['rdr']);
$de= mysqli_real_escape_string($con,$_POST['de']);
$remarks= mysqli_real_escape_string($con,$_POST['remarks']);
$f_back= mysqli_real_escape_string($con,$_POST['f_back']);
$datee= mysqli_real_escape_string($con,$_POST['datee']);
$status= mysqli_real_escape_string($con,$_POST['status']);
$tnum= mysqli_real_escape_string($con,$_POST['tnum']);



$query=mysqli_query($con,"SELECT * FROM accounts_at WHERE can_name='$can_name'")or die(mysqli_error());
$count=mysqli_num_rows($query);
if ($count>0)
{ 
    echo "<script type='text/javascript'>alert('Account already exist');</script>";
    echo "<script>document.location='index'</script>";
}
else{
    mysqli_query($con,"INSERT INTO accounts_at(id,tnum,rec,bdo,can_name,po_ti,client,rdr,de,remarks,f_back,datee,status)
        VALUES(NULL,'$tnum','$rec','$bdo','$can_name','$po_ti','$client','$rdr','$de','$remarks','$f_back','$datee','$status')")or die(mysqli_error($con));

    mysqli_query($con,"INSERT INTO accounts_at_action(id,tnum,rec,bdo,can_name,po_ti,client,rdr,de,remarks,f_back,datee,status)
        VALUES(NULL,'$tnum','$rec','$bdo','$can_name','$po_ti','$client','$rdr','$de','$remarks','$f_back','$datee','$status')")or die(mysqli_error($con));

    /*echo "<script type='text/javascript'>
    alert('Successfuly added new applicant');</script>";*/
    echo "
<script type='text/javascript'>
 setTimeout(function () { 
 swal('Successfully Added a Account!')
 },1); 
 window.setTimeout(function(){ 
  window.location.replace('index.php');
 } ,3000); 
</script>";

//  echo "<script>document.location='index'</script>";
}


?>
  <script src='https://cdn.rawgit.com/t4t5/sweetalert/v0.2.0/lib/sweet-alert.min.js'></script>

</html>

1 个答案:

答案 0 :(得分:0)

我认为您的mysqli_query发送了一些错误并使页面死亡。 “或死(mysqli_error($ con));”

因此页面不再运行至底部。 我已经清除了您的代码,而没有任何查询,只是通过PHP来回显脚本,因此效果很好。

<html>
    <link rel='stylesheet' href='https://cdn.rawgit.com/t4t5/sweetalert/v0.2.0/lib/sweet-alert.css'>
    <?php
        echo "
        <script type='text/javascript'>
        setTimeout(function () { 
            swal('Successfully Added a Account!')
        },1); 
        window.setTimeout(function(){ 
            window.location.replace('index.php');
        } ,3000); 
        </script>
        ";
    ?>
    <script src='https://cdn.rawgit.com/t4t5/sweetalert/v0.2.0/lib/sweet-alert.min.js'></script>
</html>

您可以查询查询并回显结果

echo mysqli_query($con,"INSERT INTO accounts_at(id,tnum,rec,bdo,can_name,po_ti,client,rdr,de,remarks,f_back,datee,status)
    VALUES(NULL,'$tnum','$rec','$bdo','$can_name','$po_ti','$client','$rdr','$de','$remarks','$f_back','$datee','$status')");