如何在Sweet Alert中使用AJAX

时间:2018-09-27 20:57:09

标签: php sql ajax mysqli sweetalert2

我希望用户确认他是否要更新其图像。我知道我无法在javascript中将sql与php一起使用,并且已经尝试了很多方法,但是我无法使其工作,没有显示错误,没有任何反应。此代码甚至不按按钮即可更新图像。我不了解AJAX,所以这将是我的代码及其含义的绝佳示例。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Count: <span id="count"> 0</span>

<table class="table " id="onclick">
        <thead>
          <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Count</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>John</td>
            <td>Doe</td>
            <td>1</td>
          </tr>
          <tr>
            <td>Mary</td>
            <td>Moe</td>
            <td>2</td>
          </tr>
          <tr>
            <td>July</td>
            <td>Dooley</td>
            <td>3</td>
          </tr>
          <tr>
              <td>henk</td>
              <td>janssen</td>
              <td>4</td>
          </tr>
          <tr>
              <td>piet</td>
              <td>Paulisma</td>
              <td>5</td>
          </tr>
          <tr>
              <td>Theo</td>
              <td>van gogh</td>
              <td>6</td>
          </tr>
          <tr>
              <td>Erik</td>
              <td>Doerustig</td>
              <td>7</td>
          </tr>
          <tr>
              <td>Jan</td>
              <td>de steen</td>
              <td>8</td>
          </tr>  



        </tbody>
      </table>

当用户确认后,如何正确使用ajax进行更新?

2 个答案:

答案 0 :(得分:0)

尝试以下代码段。使用preConfirm函数,并指向内部PHP文件进行数据库交互

swal({
     title: 'Remover foto de perfil?',
      showCancelButton: true,
      confirmButtonText: 'Sim, pode remover!',
      showLoaderOnConfirm: true,
      preConfirm: ()=>{
            $.ajax({
                url: 'yourPHPfile.php',
                method: 'POST',
                data:{userID:userVar},
                success: function(resp)
                      {
                        if(resp) return "ok"
                      }
            })

          }

    })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 1 :(得分:0)

通过一些出色的研究,像魅力一样工作!

    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';
                          });
                      }
            })
          }
    })
};