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.