Confirm dialog jquery ui with ajax method

1

I want to use a dialog confirm of jquery ui that is added by pressing a "save" button, that if "yes" is pressed then that a $ .ajax method is executed that saves the information in the bd. This is what I have:

$('#btnGuardaInforme').on('click',function(e){
        var comentarios = $('#comentarios').val();
        var plan = $('#plan').val();
        var periodo = $('#txtperiodo').val();
        var idIndicador = $('input#textIdindicador').val();
        var resultado = $('#txtresultado').val();
        e.preventDefault();
        $('#dialog-confirm').dialog("open");


    });

    $( function () { //DIALOG CONFIRM
        $( "#dialog-confirm" ).dialog({
                resizable: false,
                autoOpen: false,
                height: "auto",
                width: 400,
                modal: true,
                buttons: {
                 "Guardar": function() {
                        $.ajax({
                            type: 'post',
                            url: baseUrl + 'Informe/GuardaInforme',
                            data: {idIndicador: idIndicador, periodo: periodo, comentarios: comentarios, plan: plan,resultado: resultado},
                            success: function(){
                                console.log('Guardado ok');
                            },
                            error: function(){
                                console.log('error ajax al guardar informe');
                            }
                        });


                    console.log('ok confirm');
                    $( this ).dialog( "close" );
                  },
                   Cancelar: function() {
                     $( this ).dialog( "close" );
                   }
                 }
        });
    });

The function of the dialog I have outside the click event of the button and I do not know how to pass the variables sent by ajax.

    
asked by daniel2017- 04.04.2017 в 17:34
source

1 answer

0

I recommend you to do the global variables, in this way you will not have to pass them from one function to another.

Example:

var global = "variable global";

function display(local) {
 alert(global+"\n"+local);
}

display(global);

You can modify your code as follows:

var comentarios = "";
var plan = "";
var periodo = "";
var idIndicador = "";
var resultado = "";

$('#btnGuardaInforme').on('click',function(e){
        comentarios = $('#comentarios').val();
        plan = $('#plan').val();
        periodo = $('#txtperiodo').val();
        idIndicador = $('input#textIdindicador').val();
        resultado = $('#txtresultado').val();
        e.preventDefault();
        $('#dialog-confirm').dialog("open");


    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
answered by 04.04.2017 / 19:21
source