duandi8852752 2018-09-27 20:57
浏览 107
已采纳

如何在Sweet Alert中使用AJAX

I want the user to confirm if he wants to update his image. I know that I can't use sql with php inside javascript and I have tried on so many ways, but I couldn't make it work, no errors were shown, nothing happened. This code updates the image without even pressing the button. I don't have knowledge on AJAX, so it would be great an example of my code and what it means.

    <script>
function rmv_foto() {
event.preventDefault();
  swal({
    title: 'Remover foto de perfil?',
    text: "Essa ação não poderá ser desfeita.",
    type: 'warning',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#F54400',
    confirmButtonText: 'Sim, pode remover!'
    }).then((result) => {
      if (result.value) {
        var rmv_foto="<?php
        $delete = "UPDATE esc_usuarios_fotos SET img_local = 'images/user.png' WHERE img_usu_codigo = '" . $_SESSION['codigo'] . "'"; 
        mysqli_query($conexao, $delete) 
        ?>";
        swal(
          'Foto Removida!',
          'Sua foto de perfil foi removida com sucesso.',
          'success'
        ).then(function() {
          location.href = 'perfil.php';
      });
      }
    })
}
</script>

How can I properly use ajax to do the update just when user confirms?

  • 写回答

2条回答 默认 最新

  • dsxgby126001 2018-10-02 11:58
    关注

    With some good research, working like a charm!

        function rmv_foto(){
    swal({
         title: 'Remover foto de perfil?',
          showCancelButton: true,
          confirmButtonText: 'Sim, pode remover!',
          cancelButtonText: 'Cancelar',
          text: 'Essa ação não poderá ser desfeita.',
          type: 'warning',
          confirmButtonColor: '#F54400',
          showLoaderOnConfirm: true,
          preConfirm: ()=>{
                $.ajax({
                    url: 'rmv.php',
                    method: 'POST',
                    data:{},
                    success: function(resp)
                          {
                            if(resp) return "ok",
                              swal(
                                'Foto Removida!',
                                'Sua foto de perfil foi removida com sucesso.',
                                'success'
                              ).then(function() {
                                location.href = 'perfil.php';
                              });
                          }
                })
              }
        })
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀