Modal Confirmation with codeigniter

3

I need to make a modal exclusion commit.

No 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 the $ .post (base_url ... is not passing, arrives in the controller but without the id

    
asked by anonymous 29.08.2017 / 19:39

2 answers

0

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

In this way, when you click the DELETE button, you will look for the modal, and with this, when you click OK it will point to the href of the button, calling the method in question.

In the case of your question, I would do it this way:

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 part (confirmation)

    
29.08.2017 / 19:45
0

Oops, good evening, I've updated my answer a few times, see if it helps ...

FIRST OF ALL, here at

$.post(base_url + .. bla bla bla

Should not be

$.post(base_url() + bla bla bla ?? faltou o () no base_url() né?

If it's not that ..

First, trying to track the error, put alert (id_registro), to see if it has correctly received the parameter, type like this:

function Confirmar(id_registro) {
    alert(id_registro);
bootbox.confirm({

Next I would try to write in another way, because sometimes it is easier I try to find the error.

Try one of these changes:

<a id='bt-confirma' class="btn btn-primary" onclick="javascript:">Excluir</a>

or

<button id='bt-confirma' class="btn btn-primary">Excluir</a>

Then instead of using the function make use of the Jquery listener:

$(document).ready(function(){        
    $('#bt-confirma').click(function(e){
        e.preventDefault();
        var id = $('#id_registro').val();
        $.ajax({
            type: "post", //tenho costume de usar post e alterar o data
            url: base_url('index.php/ProspectoCrmController/deletar') ,
            data: 'id_registro='+id, //sei que parece da idade da pedra, mas...
            success: function(data){
                bootbox.alert('Registro excluído com sucesso.');
            }
        }
    });
});
    
02.09.2017 / 23:35