javascript - How to get multiple responses from PHP file via AJAX? - Stack Overflow

My PHP file doing 2 operations: 1. Submits data from form into db table, 2. Sends email.What I want to

My PHP file doing 2 operations: 1. Submits data from form into db table, 2. Sends email.

What I want to do is to show status messages via ajax. For example, "First operation done, please wait for second" and then when second one will be finished show the next message "Second operation done too". Now my ajax looks like that.

How can I modify it?

//add status data to form
        form.data('formstatus', 'submitting');

        if (validate()) {
            //send data to server for validation
            $.ajax({
                url: formUrl,
                type: formMethod,
                dataType: "json",
                data: formData,
                success: function (data) { 
                    $.notifyBar({
                        cls: data.status,
                        html: data.message
                    });
                    form.data('formstatus', 'idle');
                }
            });

        }

My PHP file doing 2 operations: 1. Submits data from form into db table, 2. Sends email.

What I want to do is to show status messages via ajax. For example, "First operation done, please wait for second" and then when second one will be finished show the next message "Second operation done too". Now my ajax looks like that.

How can I modify it?

//add status data to form
        form.data('formstatus', 'submitting');

        if (validate()) {
            //send data to server for validation
            $.ajax({
                url: formUrl,
                type: formMethod,
                dataType: "json",
                data: formData,
                success: function (data) { 
                    $.notifyBar({
                        cls: data.status,
                        html: data.message
                    });
                    form.data('formstatus', 'idle');
                }
            });

        }
Share Improve this question edited Aug 17, 2019 at 11:47 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Nov 12, 2011 at 13:03 Tural AliTural Ali 23.3k18 gold badges81 silver badges133 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

in the success block you can perform another ajax call. That's the simplest. You can do it to in .success(), .ajaxSucces(), .plete(), or .then() function like this: $.ajax(...).success(...);

ideally you would embed the code in a function, by example

$.ajax({
    url: formUrl,
    type: formMethod,
    dataType: "json",
    data: formData,
    success: function (data) {
        notifyResponse(data); 
        form.data('formstatus', 'idle');
        sendMail();
    }
});

function sendMail() {
    $.get(mailUrl, function(data) {   // or $.post(...
        notifyResponse(data);
    });
}

function notifyResponse(data) {
    $.notifyBar({
        cls: data.status,
        html: data.message
    });
}

If you've to do two operations that have different execution times, just send two different AJAX queries, and get the responses from them.

Divide your PHP service in two parts. If the second part depends on the first, instead of sending the two requests at the same time, send the second request when the first one returns.

In other words, in your success callback, you're going to notify the user that the first operation has been pleted and you proceed to call the second operation, whose success callback will inform that the second operation has been pleted.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744275777a4566323.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信