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 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答