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 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路