Confirmation Modal with codeigniter

Asked

Viewed 785 times

3

I need to do an exclusion confirmation modal.

In html

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

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 is going on? the id passed by the delete button, arrives without errors in the confirm function, but in $.post(base_url... is not passing, enough in the controller but without the id

2 answers

0

Do it this way:

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

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

In the case of your question, I’d 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 part of if(confirmation)

  • 1

    Okay, this will work! But I need to make it work within the framework , so in jquery you have to call the controller (even if you’re doing it) , but you have to take the variable together and you’re not going... , so what you did in this case doesn’t solve by not using a framework but still thanks for the help : )

  • Yes, that way I use in codeigniter, but I edited for a response to your code, how I would do... post also delete from your controller

  • 1

    I got it... I’m gonna test it

  • 1

    has some strange button.. it does not arrive in jquery... arrives in modal, shows confirmation and does not go to $('#confirms-delete

  • If he opened the modal, he called the click function. Where is wrong?

    1. The button the button makes the call to the modal: data-target="#confirm-delete" 2.The modal at this time the modal appears showing the cancel option or "Yes, I want to delete" <div class="modal fade"id="confirms-delete" 3. The Death for all there it does not arrive in jquery $('#confirms-delete' nor in function Function deleting($id)
Show 1 more comment

0

Oops, good night, updated my answer a few times, see if it helps you...

FIRST OF ALL, HERE IN

$.post(base_url + .. bla bla bla

shouldn’t be

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

Otherwise..

First, trying to chase the error, put Alert(id_record), to see if you received the parameter correctly, like this, :

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

Then I would try to write differently, because sometimes it’s easier that 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>

There instead of using the function make use of the Jquery System:

$(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.');
            }
        }
    });
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.