使用swal

时间:2018-11-06 10:25:31

标签: javascript php html

在我的页面之一上有一个表单,我也正在使用Swal要求用户在执行操作之前确认其操作(删除文件)。 这是Swal代码:

<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>
这是我的提交按钮:

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

这是我的删除代码:

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

如果您在这里发现任何搞笑的错误,请原谅我的诺言<< / p>

尝试了我发现的所有建议,例如使用许多用户建议的isConfirm函数。但是没有一个有效。

编辑

所以我尝试测试文件以查看是否出错,所以这是主文件上使用的代码:

<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>

用于连接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);
?>

我收到错误日志:

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)

1 个答案:

答案 0 :(得分:0)

好吧,我将按钮ID设置为“ B1” 然后我添加了代码:

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()
				},
			});

		}

	}

看起来像是在我还没确定按钮操作之前……对我来说是个工作包