如何通过AJAX获得PHP文件多选? [英] How to get multiple responses from PHP file via AJAX?

查看:105
本文介绍了如何通过AJAX获得PHP文件多选?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的PHP文件做2操作:1.提交的数据表单到数据库表,2。发送电子邮件。我想要做的就是通过AJAX显示状态信息。对于前:第一次操作完成的,请等待第二个然后当第二个将完成节目的下一条消息二运做了。。现在我的ajax样子的。如何修改呢?

  //添加状态数据形成
        form.data('formstatus','提交');

        如果(验证()){
            //将数据发送到服务器进行验证
            $阿贾克斯({
                网址:formUrl,
                类型:formMethod,
                数据类型:JSON,
                数据:FORMDATA,
                成功:功能(数据){
                    $ .notifyBar({
                        CLS:data.status,
                        HTML:data.message
                    });
                    form.data('formstatus','闲置');
                }
            });

        }
 

解决方案

在成功的块可以执行的另一个的AJAX调用。这是最简单的。你可以把它做的.success(),.ajaxSucces(),.complete(),或。那么()函数是这样的:$。阿贾克斯(...)成功(...);

在理想情况下嵌入code函数中,通过实例

  $。阿贾克斯({
    网址:formUrl,
    类型:formMethod,
    数据类型:JSON,
    数据:FORMDATA,
    成功:功能(数据){
        notifyResponse(数据);
        form.data('formstatus','闲置');
        发送邮件();
    }
});

功能的sendmail(){
    $获得(mailUrl,功能(数据){//或$。员额(...
        notifyResponse(数据);
    });
}

函数notifyResponse(数据){
    $ .notifyBar({
        CLS:data.status,
        HTML:data.message
    });
}
 

My PHP file doing 2 operations: 1. Submits data from form into db table, 2. Sends email. What I wanna do is to show status messages via ajax. For ex: "First operation done, please wait for second" and then when second one will be finished show the next message "Second op. 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');
                }
            });

        }

解决方案

in the success block you can perform another ajax call. That's the simplest. You can do it to in .success(), .ajaxSucces(), .complete(), 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
    });
}

这篇关于如何通过AJAX获得PHP文件多选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆