Sweetalet does not work well when deleting a record from a table

0

I have a dataTable with a delete button and I am implementing the sweetalert library. The problem is that clicking on the delete button shows the modal sale of the sweetalerty but does not eliminate.

What could be the problem?

The Code

//============== Las liberías ==============

<script type="" src="plugins/datatables/jquery-1.12.4.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css" rel="stylesheet"/>

// ========= El botón ===============

<a href="#" onclick="confirmDelete()" title="Eliminar" class="glyphicon glyphicon-trash btn btn-danger btn-sm"></a>

//======= El javaScript ==============

<script type="text/javascript">
function confirmDelete() {
    swal({
        title: "Are you sure?",
        text: "You will not be able to recover this imaginary file!",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes, delete it!",
        closeOnConfirm: false
    }, function (isConfirm) {
        if (!isConfirm) return;
        $.ajax({
            url: "ruta-donde-elimino.php",
            type: "POST",
            data: {
                id: 5
            },
            dataType: "html",
            success: function () {
                swal("Done!", "It was succesfully deleted!", "success");
            },
            error: function (xhr, ajaxOptions, thrownError) {
                swal("Error deleting!", "Please try again", "error");
            }
        });
    });
}
</script> 

// código d ela clase personaData.php

public function del(){
		$sql = "delete from ".self::$tablename." where idPersona=$this->idPersona";
		Executor::doit($sql);
	}
  
  
  //Código d ela clase persona-action.php
  
  else if (isset($_GET["opt"]) && $_GET["opt"]=="del"){
	$persona = new personaData();
	$persona->idPersona = $_GET["idPersona"];
	$persona->DEL();
	header("Location: ./?view=persona&opt=all");
}

The question I have is what the url would be where it says: url: "path-where-delete.php", taking into account the way I delete the records

    
asked by Horus 08.02.2018 в 17:18
source

1 answer

0

You are doing everything wrong, because you have a link that executes the action to leave your environment and at the same time execute the alert, what you should do is eliminate the redirection of the link and pass it to alert:

<a href="#" title="Eliminar" class="glyphicon glyphicon-trash btn btn-danger btn-sm eliminar"></a>

and in the alert, we detect the click and ask for the confirmation:

$(".eliminar").click(function(e){
    e.preventDefault();
  swal({   title: "¡El registro será eliminado permanentemente!",   
    text: "¿Estás seguro de proceder?",   
    type: "warning",   
    showCancelButton: true,   
    confirmButtonColor: "#DD6B55",   
    confirmButtonText: "¡Eliminar Registro!",   
    cancelButtonText: "¡No Eliminar!",   
    closeOnConfirm: false,   
    closeOnCancel: false }, 
    function(isConfirm){   
        if (isConfirm) 
        {   
            //Envio la eliminación
            window.location.href="./?action=persona&opt=del&idPersona=<?=$per->idPersona;?>"

        } 
        else {     
            //Cancelo al eliminacioon
            swal("Hurray", "Account is not removed!", "error");   
        } });
})
    
answered by 08.02.2018 в 19:22