dougang2749 2018-11-06 10:25
浏览 75
已采纳

使用swal删除对sql的操作

There's a form on one of my pages for which also I'm using Swal to ask the user to confirm his action (deleting a file) before making it happen. Here's the Swal code:

<script>
    function apagar(id) {
        
        swal({
            title: "Atenção",
            text: "Deseja eliminar o pedido com o id : " + id + " ?",
            type: "warning",
            showCancelButton: true,
            CancelButtonColor: "#d33",
            confirmButtonColor: "#d33",
            confirmButtonText: "Eliminar",
            cancelButtonText: "Cancelar",
            closeOnConfirm: false
        }, function () {
            swal("Pedido " + id + " eliminado com sucesso.", "Continuação de um bom trabalho.",
                "success");
            $.ajax({
                type: "POST",
                url: 'apagar.eventos.php',
                data: {
                    id: id

                },
                success: function (html) {
                     
                    window.location.href = 'index.php';
                }
            });
        });
        return false;
    }
</script>

And this is my submit button:

<button type="button"  id="<?php echo $idpedido;?>" onclick="apagar(this.id)" class="btn btn-warning">Cancelar Agendamento</button>

And this is my delete code:

<?php
include("db.connect.php");
$id2 = $_POST['id'];
$sql = "DELETE FROM nagenda WHERE id = '$id2' ";
$stmt = sqlsrv_query( $conn, $sql );
sqlsrv_close($conn);
?>

if you notice any hilarious mistake here, pardon my noobieness <

tried out all the suggestions that I found, like using the isConfirm function suggested by many users. But none worked.

EDIT

so i try on test files to see if i got error, so here is the code used on main file :

<div id="mydiv" class="container col-md-5">
<link href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
<?php $idpedido="1145";?>

<button type="button" id="<?php echo  $idpedido;?>" onclick="apagar(this.id)" class="btn btn-warning">Cancelar Agendamento</button>

<script>
    function apagar(id) {
        
        swal({
            title: "Atenção",
            text: "delete id : " + id + " ?",
            type: "warning",
            showCancelButton: true,
            CancelButtonColor: "#d33",
            confirmButtonColor: "#d33",
            confirmButtonText: "yes",
            cancelButtonText: "no",
            closeOnConfirm: false
        }, function () {
            swal("id " + id + " have been deleted.",
                "success");
                $.ajax({
                type: "POST",
                url: 'teste2.php',
                data: {
                    id: id

                },
                success: function (html) {
                    include("teste2.php");
                }
            });
        });
        return false;
    }
</script>
<script src="vendors/bower_components/sweetalert/dist/sweetalert.min.js"></script>

code used to conect tu bd sql:

<?php
$id2 = $_POST['id'];

include("agenda\dist\db.connect.php");
 
$sql = "DELETE FROM nagenda WHERE id = '$id2' ";
$stmt = sqlsrv_query( $conn, $sql );
sqlsrv_close($conn);
?>

i'm geting error log :

Uncaught ReferenceError: $ is not defined
at Object.doneFunction (texte.php:23)
at l (sweetalert.min.js:1)
at Object.s [as handleButton] (sweetalert.min.js:1)
at HTMLButtonElement.g (sweetalert.min.js:1)
</div>
  • 写回答

1条回答 默认 最新

  • douyi3760 2019-01-07 15:16
    关注

    well i set the button id to "B1" then i added the code:

    var clientId = document.getElementById('b1');
    
        clientId.onclick = function () {
    
            alertMessage(clientId.value);
    
        }
    
        function alertMessage(clientID) {
            swal({
                title: "Deseja eliminar o Agendamento?",
                text: "Se eliminar o agendamento não será possivel recuperar.",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#f8b32d",
                confirmButtonText: "Eliminar",
                cancelButtonText: "Cancelar",
                closeOnConfirm: false
            }, function () {
                swal("Agendamento liminado com sucesso.","success");
                sendAjax(clientID);
            });
    
            function sendAjax(clientID) {
    
                $.ajax({
                    type: "POST",
                    url: "dist/apagar.eventos.php",
                    data: {
                        id: clientID
                    },
                    success: function (data) {
                        console.log(data)
                        window.location.reload()
                    },
                });
    
            }
    
        }

    looks like before i wasant indentified the button action..., worket for me

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题