Ajax call by POST executes php function twice

2

Good morning, I have a problem with an ajax call to a controller method. The problem is that when the method is executed, I record the same records in DB twice. I would be very grateful if anyone could shed some light on the matter.

Ajax Code:

<?php 
$script = <<< JS

    //estado inicial WorkFlow
    estado_origen = $('#orden-status').val();

    $('#w0').submit(function(event){
        event.preventDefault();
        event.stopImmediatePropagation();

        //estado seleccionado WorkFlow
        select = $('#orden-status').val();    

        // De EnCurso a PendienteRealizar:
        if (estado_origen == 'OrdenWorkflow/encurso' && select == 'OrdenWorkflow/pendienterealizar') {
             $('#modalEncursoPendiente').modal('show');
             $('#btn-modalEncursoPendiente-aceptar').click(function() {
                if($('#motivosvueltavisita').val() != ''){
                    var motivo = $("<input>")
                                 .attr("type", "hidden")
                                 .attr("name", "Orden[motivo]").val($('#motivosvueltavisita').val());
                    $('#w0').append($(motivo));
                    var obs = $("<input>")
                                 .attr("type", "hidden")
                                 .attr("name", "Orden[obs]").val($('#orden-obs').val());
                    $('#w0').append($(obs));
                }
                $('#modalEncursoPendiente').modal('hide');            
                $('#w0').unbind('submit').submit();
            });
        }
        // De EnCurso a Fin Conformidad: ***AQUI ESTA LA LLAMADA AJAX QUE ME DA PROBLEMA***
        else if (estado_origen == 'OrdenWorkflow/encurso' && select == 'OrdenWorkflow/finconfirmadas') {
            $('#modalConfirmadas').modal('show');
            $('#btn-modalConfirmadas-aceptar').click(function() {
                var keys = $('#exclusiones-grid').yiiGridView('getSelectedRows');           
                $.ajax({ 
                    url: '/orden/exclusiones/?id='+ $model->id_orden, 
                    type: 'post', 
                    data: {keylist: keys},
                    dataType: "json",
                    success: function(data){  
                        $('#modalConfirmadas').modal('hide');            
                        //$('#w0').unbind('submit').submit();
                    }, 
                    error: function (xhr, ajaxOptions, thrownError) { 

                    }       
                });             
            });
        }
        else{
            $('#w0').unbind('submit').submit();   
        }
    });

    $('#btn-modalEncursoPendiente-cerrar').click(function() {
        $.pjax.reload({container:'#pjax-orden'});
    });

    $('#btn-modalConfirmadas-cerrar').click(function() {
        $.pjax.reload({container:'#pjax-orden'});
    });
JS;
$this->registerJs($script); 
?>

and the driver code:

public function actionExclusiones($id)
    {    
        if (isset($_POST['keylist'])) {

            $keys = $_POST['keylist'];

            foreach ($keys as $value) {
                $exclusiones = new ROrdenExclusion();
                $exclusiones->id_exclusion = $value;
                $exclusiones->id_orden = $id;
                if (!$exclusiones->save()) {
                    return false;
                    break;
                }
            }  
            return true;          
        }        
    }

Thank you very much in advance.

    
asked by In0cybe 08.08.2017 в 15:32
source

1 answer

0

It seems that when doing post the cancellation of the action of the event "preventDefault ()" is lost. I added a second event at the click of the button and that does not make the ajax call twice.

else if (estado_origen == 'OrdenWorkflow/encurso' && select == 'OrdenWorkflow/finconfirmadas') {
        $('#modalConfirmadas').modal('show');
        $('#btn-modalConfirmadas-aceptar').click(function(e) {
            e.preventDefault(); //segunto evento de cancelación
            e.stopImmediatePropagation();
            var keys = $('#exclusiones-grid').yiiGridView('getSelectedRows');           
            $.ajax({ 
                url: '/orden/exclusiones/?id='+ $model->id_orden, 
                type: 'post', 
                data: {keylist: keys},
                dataType: "json",
                success: function(data){  
                    $('#modalConfirmadas').modal('hide');            
                    $('#w0').unbind('submit').submit();
                }, 
                error: function (xhr, ajaxOptions, thrownError) { 

                }       
            });             
        });
}
    
answered by 08.08.2017 / 18:34
source