Show Notification with toastr.js in file where it will be redirected after success

0

I have the following concern. I have a form that is sent through ajax and validated before its submission with formvalidation. Everything works fine, but I need the notification to be shown in the file where it will be redirected after the ajax success.
The first notification window is generated through SWAL and there asks if you want to send or not the order and after that, generates the notification with toastr. This is my code:

<script>

$(document).ready(function() {

var newDate = new Date(Date.now() + <?php echo $dia_max?>*24*60*60*1000);

$('#datePicker').datepicker({
    format: 'dd/mm/yyyy',
    autoclose: 'true',
    language: 'es',
    daysOfWeekDisabled: [0, 6],
    startDate : newDate
})
    .on('changeDate', function(e) {
        $('#envia_pedidoE').formValidation('revalidateField', 'date');
    });
});

$('#envia_pedidoE').formValidation({
    framework: 'bootstrap',
    excluded: ':disabled',

    fields: {
        obs: {
            validators: {
                notEmpty: {
                    message: 'Ingrese Obs. de Pedido.'
                }
            }
        },
        date: {
            validators: {
                notEmpty: {
                    message: 'Seleccione Fecha'
                },
                date: {
                    format: 'DD/MM/YYYY',
                    message: 'Formato Fecha NO VALIDO'
                }
            }
        }
    }
}).on('success.form.fv', function(e) {

        $(document).ready(function(){
        $('#envia_pedidoE').submit(function(e){
            e.preventDefault();
            var datos = $(this).serialize();

            $.ajax({
                type:"POST",
                url: "envioE.php",
                data : datos,

                success:function(data){
                var href = "archivoderedireccion.php";
                swal({
                 title: "Realizará Envío de Pedido",
                 text: "Para Usuario",
                 icon: "info",
                 buttons: true,
                 dangerMode: true,
                })
               .then((envio) => {
                 if (envio) {
                  $.toast({
                  heading: 'Envío de Pedido',
                  text: 'Proceso Realizado con Exito. El Pedido Fué Enviado Correctamente.',
                  position: 'top-right',
                  loaderBg: '#ff6849',
                  icon: 'success',
                  hideAfter: 4500,
                  stack: 6
                });
                  window.location.href = href;
              } else {
                 $.toast({
                 heading: 'Envío de Pedido',
                 text: 'Se Ha Cancelado en Envío del Pedido.',
                 position: 'top-right',
                 loaderBg: '#ff6849',
                 icon: 'error',
                 hideAfter: 4500
              });
            }
            });
            $('#respuesta').html(data);
            }
            });
        });
    });
});

As I was saying, the above works but the message disappears immediately and what I need is to do a redirection to file addressirection.php and in that file the toastr notification is displayed.

If anyone has any idea how to do it or guide me in any way, I would greatly appreciate it. As always grateful in advance, for your help.

Greetings to all.

    
asked by maha1982 04.12.2018 в 20:04
source

1 answer

0

I think you can do it like this:

  • If you do a previous validation, add in the data to take this validation. With something simple, type:
  

var validation = {'success': 0, 'message': 'blablabla'};

Enough, or the message you determine already on the page to which you redirect. That already customizalo as you want.

  • On the page where you receive the data, pass the data you receive from the validation to js, make yourself a function that takes out the toastr in case the validation does not arrive as expected, and invoke it as soon as you receive the data:
function showToastr(data){
    if(!data.success){
       toastr.warning(data.message);
    }
}
showToastr(datos_recibidos.validation);

Then, making a function a little better you can customize it as you want: infos, warning, etc ..

    
answered by 04.12.2018 в 20:43