0

I'm using jQuery ajax call to post process a form.
I want to display a loading message or image while the form is processed and when the action is completed to display a complete message.
How can I do it?
This is my jQuery code.

$s('body').on('click', '#group-update', function() {
    var formInputs = $s('input').serializeArray();
    var groupId = $s(this).data('group');
    var error = $s('#modal .info');
    var tr = $s('#dataT-attrgroup').find('tr.on_update');

    formInputs.push({
        name: 'id',
        value: groupId
    });

    $s.ajax({
        type: 'post',
        url: 'index.php?controller=attribute&method=updateGroup',
        data: formInputs,
        dataType: 'JSON',
        success: function(data) {
            if(data.response === false){
                error.addClass('info-error');
                error.html(data.message);
            }else{
                oTable.row(tr).data(data).draw();
                $s('#modal').modal('hide');
                tr.removeClass('on_update');
                $s.growl.notice({
                    title: 'Success',
                    message: 'Grupul de atribute a fost actualizat'
                });
            }

        }
    });
});
0

4 Answers 4

1

Before ajax function display your loader and inside the success function from your ajax hide it.

As you can see in my example i inserted $('.loader').show(); and $('.loader').hide();

$('.loader').show();
$s.ajax({
        type: 'post',
        url: 'index.php?controller=attribute&method=updateGroup',
        data: formInputs,
        dataType: 'JSON',
        success: function(data) {
            if(data.response === false){
                error.addClass('info-error');
                error.html(data.message);
            }else{
                oTable.row(tr).data(data).draw();
                $s('#modal').modal('hide');
                tr.removeClass('on_update');
                $s.growl.notice({
                    title: 'Success',
                    message: 'Grupul de atribute a fost actualizat'
                });
            }
            $('.loader').hide();
        }
    });
Sign up to request clarification or add additional context in comments.

3 Comments

my for is validated before processed
the loading message should appear while the php call is made
i believe there are some advanced techniques for this, like file uploading through ajax but if you want to keep it simple then i`d suggest two options: 1. make the checking in js, 2. create 2 ajax requests
0

According to the PHP docs:

The upload progress will be available in the $_SESSION superglobal when an upload is in progress, and when POSTing a variable of the same name as the session.upload_progress.name INI setting is set to. When PHP detects such POST requests, it will populate an array in the $_SESSION, where the index is a concatenated value of the session.upload_progress.prefix and session.upload_progress.name INI options. The key is typically retrieved by reading these INI settings, i.e.

You should take a look at : https://github.com/blueimp/jQuery-File-Upload/wiki/PHP-Session-Upload-Progress

I think this will definitely help you out!

Comments

0

Display your message just before launching $.ajax(); And close it in the success (and error) callback functions. example :

$s('body').on('click', '#group-update', function() {
var formInputs = $s('input').serializeArray();
var groupId = $s(this).data('group');
var error = $s('#modal .info');
var tr = $s('#dataT-attrgroup').find('tr.on_update');

formInputs.push({
    name: 'id',
    value: groupId
});

var dlg = $s('<div/>').text('your message').dialog();

$s.ajax({
    type: 'post',
    url: 'index.php?controller=attribute&method=updateGroup',
    data: formInputs,
    dataType: 'JSON',
    error:function() {
        dlg.dialog('close');
        },
    success: function(data) {

        dlg.dialog('close');

        if(data.response === false){
            error.addClass('info-error');
            error.html(data.message);
        }else{
            oTable.row(tr).data(data).draw();
            $s('#modal').modal('hide');
            tr.removeClass('on_update');
            $s.growl.notice({
                title: 'Success',
                message: 'Grupul de atribute a fost actualizat'
            });
        }

    }
});
});

Comments

0

If you go through ajax section of jquery documentation you will notice some more method like success ie error, beforesend, complete etc. Here is the code snippet.

$s.ajax({
  type: 'post',
  url: 'index.php?controller=attribute&method=updateGroup',
  data: formInputs,
  dataType: 'JSON',
  beforeSend : function(){
    // load message or image
  },
  success: function(data) {
    // write code as per requirement
  },      
  complete : function(){
    // load complete message where you previously added the message or image, as a result previous one will be overwritten
  }  
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.