在基于 Ajax 的表单提交中显示不同的消息


Show different messages on Ajax based form submission

我正在开发一个脚本来处理基于 AJAX 的表单提交。我能够使用 AJAX 提交表单并显示成功消息。但是,我想根据后台表单数据处理显示不同的消息。例如,当用户注册表提交时,它被输入到 2 个系统中,所以我想显示 - 用户在系统 1 中创建,然后在系统 2 中创建用户,然后正在发送电子邮件,所以我想显示正在发送电子邮件。 等等。

我目前正在使用 jquery ajax 方法。 这是我的示例代码。

formData = jQuery('form#Registration').serialize();
 jQuery.ajax({
            url: 'form_process.php',
            type: 'POST',
            data: formData,
            dataType: 'html',
            success: function (msg, textStatus, xhr) {
                jQuery('#success_msg').html( msg );
            }
        });

那么我如何根据数据处理显示不同的消息。

谢谢

因此,为此,您需要在服务器端进行处理,然后您可以回显要显示的消息:

if(condition){
    echo "User created in system One.";
} else if(condition){
    echo "User created in system Two.";
}else{
    echo "Email sent.";
}

现在,在jQuery.ajax()您需要将dataType更改为:

dataType:"text"

这里有登录.js。 我们将从登录表单中获取数据。 我想你会从给定的代码中理解的。

  var adminlogin={
    init:function(){
        this.formSubmitEvent();
    },
    formSubmitEvent:function(){
        _this=this;
        $("#adminlogin-form").submit(function(){
            if((result=formValidation("adminlogin-form"))&&(result['error']))
            {
                return false;
            }
            data=getFormObj("adminlogin-form");
            _this.login(data);
            return false;
        });
    },
    login:function(data){
        $.ajax({
            url:"admin/login.php",
            data:data,
            success:function(data){
                if(data.adminid)
                {
                    window.location = "home.php";
                }else
                {
                    showError(data.reason);
                }
            },
            error:function(data){
                if(data.responseText=="")
                {
                    showError("Some error occured in server");
                }
                else
                {
                    showError(data.responseText);
                }
            }
        });
    }
};
adminlogin.init();

像这样尝试