首页 > 解决方案 > 如何用 SweetAlert 替换此 Ajax 警报?

问题描述

我是在这里寻找解决方案的长期浏览器,但从未发布过……直到现在!

我尝试了各种变化,我确信这很简单,但我被卡住了。我有以下 Ajax 脚本来更新我的数据库中的记录。

<button class="btn btn-dark" type="submit" id="update"><i class="fas fa-edit"></i> UPDATE</button>

<script>
  $(document).ready(function(){
      $("#update").click(function(){
          var fname=$("#fname").val();
          var thestore=$("#thestore").val();
          $.ajax({
              url:'update.php',
              method:'POST',
              data:{
                  fname:fname,
                  thestore:thestore 
              },
              success:function(response){
                  alert(response);
              }
          });
      });
  });
  </script>

我正在尝试用 SweetAlert替换功能(响应)部分,但由于警报功能上方的附加代码,我感到困惑。以下是我想放在那里的 SweetAlert 代码:

swal("Good job!", "You have updated the record!", "success")

任何人都可以帮助我如何最好地将其合并到我现有的 Ajax 代码中吗?

这是 update.php 进程结束并显示警报的地方:

    $conn = new mysqli('localhost', 'root', '', 'my_db');
$name=$_POST["fname"];
$thestore=$_POST["thestore"];
$sql="UPDATE feedback set firstName='$name', store='$thestore' where id=1";
if($conn->query($sql)===TRUE){
echo "You have updated the record!"; }

谢谢 :)

标签: mysqlajaxsweetalert

解决方案


恭喜您在 Stack Overflow 上提出第一个问题!

我想你可以这样写:

<button class="btn btn-dark" type="submit" id="update"><i class="fas fa-edit"></i> UPDATE</button>

<script>
  $(document).ready(function(){
      $("#update").click(function(){
          var fname=$("#fname").val();
          var thestore=$("#thestore").val();
          $.ajax({
              url:'update.php',
              method:'POST',
              data:{
                  fname:fname,
                  thestore:thestore 
              },
              success:function(response){
                  // alert(response);
                  swal("Good job!", response, "success");
              }
          });
      });
  });
  </script>

推荐阅读