How do I make the Alert() method in JS appear only 1 time when the user logs into the application?

Asked

Viewed 303 times

0

The idea is this, when the user enters the application will appear to him the messages registered in the bank, but I want to appear only 1 time for him. And when he clicks to close the Alert() text box, I want you to do an INSERT in the database by passing the user, date and time of the display. I need help !!

* Script.js* - Checks for messages and returns via AJAX

$('#form-msg').ready(function(){


    var form = $('#form-msg');
    var callback = form.find('input[name="callback"]').val();
    var callback_action = form.find('input[name="callback_action"]').val();
    var usuario = form.find('input[name="message_responsible"]').val();
    var type = form.find('input[name="message_type"]').val();
    //console.log(type);

    $.ajax({
        method: 'POST',
        url: 'themes/intranet_fametro/ajax/' + callback + '.ajax.php',
        data: {
            'callback': callback,
            'callback_action': callback_action,
            'message_responsible': usuario,
            'message_type': type
        },
        dataType: 'json',
        success: function(data){
            var obj = eval(data);
            obj.forEach(function(e){
                alert('Mensagem: ' + e.message_message);
            });
        }                     
    });
})

3 answers

0

Following the question of the title and considering the explanation, I understood that these messages are viewed only once, so: Create an Ajax request to your backend by checking that the message view log has already been entered and, if it has not been inserted, you make a new request to load the messages and then display them as you have already done.

In question the use of Alert ,I believe it is not what you are looking for, because you cannot control when the window has actually been closed. As already said in the previous comment, the right would be the use of confirm().

Following is documentation with examples of use: https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm

  • Pasta, I’ll try it ! Thank you

0

The warning dialog should be used for messages other than require no response from the user other than the acknowledgement of the message. source

A resource you could use to replace Alert is with confirm, the only problem is that the user will have the option to cancel, but with confirm it is possible to receive the user’s reply:

const response = confirm('Mensagem: ' + e.message_message);

with the answer you can work on a new ajax call to your backend and thus perform the Insert in your database.

  • Bacana, thanks for the tip, helped a lot clearing the ideas !

0


Rewrite the method, storing errors in an array, to display them all once.

    success: function(data){
      const message = [];
      var obj = eval(data);
      obj.forEach(function(e){
        message.push(e.message_message);
      });

      alert('Mensagem: ' message.toString());
    }
  • Thank you very much! helped me a lot

Browser other questions tagged

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