Confirmation Mode with codeigniter

Question:

I need to make a modal of deletion confirmation.

in html

<a class="btn btn-primary" onclick="Confirmar(<?=$dados->id;?>)">Excluir</a>

The function

<script>
var base_url = '<?php echo base_url() ?>';

function Confirmar(id_registro) {
    bootbox.confirm({
        message: 'Confirma a exclusão do registro?',
        callback: function (confirmacao) {

            if (confirmacao) {
                $.post(base_url + 'index.php/ProspectoCrmController/deletar',{
                    id_registro: id_registro
                }, 'json');
                bootbox.alert('Registro excluído com sucesso.');
            }else {
                bootbox.alert('Operação cancelada.');
            }

        },
        buttons: {
            cancel: {label: 'Cancelar', className: 'btn-default'},
            confirm: {label: 'EXCLUIR', className: 'btn-danger'}

        }
    });
}

What happens? the id passed by the delete button arrives without errors in the confirm function, but in $.post(base_url… it's not passing, it arrives in the controller but without the id

Answer:

Do as follows:

HTML:

<div class="modal fade" id="confirma-deletar" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Alerta</h4>
            </div>

            <div class="modal-body">
                <p>Você realmente deseja deletar este registro?</p>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                <a class="btn btn-danger btn-ok">Sim, quero deletar!</a>
            </div>
        </div>
    </div>
</div>

EXCLUSION BUTTON:

<button type="button" class="btn btn-default btn-xs" name="deletar" data-toggle="modal" data-target="#confirma-deletar" data-href="<? echo base_url("assistenciatecnica/deletando/".$valor->ass_id); ?>" data-id="<?php echo $valor->ass_id; ?>">DELETAR</button></a></button>

JQUERY:

$('#confirma-deletar').on('show.bs.modal', function(e) {
    $(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
});

DELETING METHOD:

function deletando($id){
    $retorno = $this->model_implantacao->deletar($id);
    if($retorno==true){
        $this->session->set_flashdata('sucesso', 'Registro deletado com sucesso!');
    }else{
        $this->session->set_flashdata('error', 'Erro ao deletar registro!');
    }
    echo redirect(base_url("implantacao"));
}

This way, when you click on the DELETE button, it will look for the modal, and with that, when you click on OK it will direct the button's href, calling the method in question.

In the case of your question, I would do it as follows:

if(confirmacao){
    $.ajax({
        type: "json",
        url: base_url + "index.php/ProspectoCrmController/deletar",
        data: {id_registro:id_registro},
        success: function(data){
            bootbox.alert('Registro excluído com sucesso.');
        }
    }); 
} else {
    bootbox.alert('Operação cancelada.');
}

Changing the if(confirmation) part

Scroll to Top