yii2 ActiveForm在提交表单时调用ajax

时间:2015-08-03 08:48:05

标签: php ajax yii2

我的功能的一些描述:

  • 当用户提交表单时,应用程序会将数据保存到数据库,然后将邮件发送给系统中的某个人。我想将保存过程和sendmail过程分为两个单独的过程。

  • 我正在构建一个基于ActiveForm类的表单。我想在提交表单时发出一个ajax请求来处理sendmail进程。我有一段javascript代码:

function sendMail()
    {
        $("<?php echo $form ?>").on("beforeSubmit", function (event, messages) {
            $.ajax({
                url: '/site/send-mail-ajax',
                method: 'POST',
                data: {template: "<?php echo $template; ?>", vars: '<?php echo $vars ?>', from: "<?php echo $from ?>", to: '<?php echo $to ?>', subject: "<?php echo $subject; ?>"},
                success: function (res)
                {
                    if (res.sent) {
                        console.log('Email was sent.');
                    }
                },
                error: function (res)
                {
                    alert('something is wrong.')
                }
            });
        });
    }

在我的代码上面,我仍然需要在提交之前等待ajax请求的完成。 怎么能解决这个问题呢? 任何建议都将受到重视。

0 个答案:

没有答案