邮件发送之前从7开始验证联系人

时间:2016-12-08 18:10:06

标签: wordpress wordpress-theming contact-form-7

我想查看提交的地址是否有正确的地址然后发送电子邮件。我的代码工作但我希望在7次验证联系后执行我的代码。如果我的代码返回true,则表单将提交并发送电子邮件,否则显示警告,不应提交。

$('.wpcf7-submit').on('click', function (e) {
    var address = $('input[name="your-address"]').val();
    //e.preventDefault(); 
    if(city !=="") {
        jQuery.post(gs_cf7_url, {data:address, action:'gs_cf7_check_lat_lag'}, function (response){
            if(response=="no") {
                alert('Sorry we couldnt find the location');
            } else {
                //$(".wpcf7-submit").unbind("submit").submit(); 
                //$(".wpcf7-submit").unbind("submit");
            }
        });
    }
}); 

如果有任何javascript钩子,我正在寻找add_action('wpcf7_before_send_mail', 'save_form');之类的钩子?

1 个答案:

答案 0 :(得分:0)

Contact From 7的逻辑如下。在提交点击它点击ajax,在PHP代码中执行所有验证并返回到js内部函数$ .wpcf7AjaxSuccess = function(data,status,xhr,$ form)。此函数检查data.invalids,如果为true,则显示错误消息。

这意味着在联系表单7验证后执行检查为时已晚,因为当php代码接受所有数据有效时,它会发送电子邮件并将控制权返回给$ .wpcf7AjaxSuccess。

作为一种变通方法,您可以在CF7中标记地址字段,在您的字段上监视.focusout()事件(输入[name =“your-address”])并在输入的地址无效时清除它。提交点击后,CF7将不会发送电子邮件,并且需要您的字段。

尝试以这种方式更改代码:

$('input[name="your-address"]).focusout( function () {
    var address = $('input[name="your-address"]').val();
    //e.preventDefault(); 
    if(city !=="") {
        jQuery.post(gs_cf7_url, {data:address, action:'gs_cf7_check_lat_lag'}, function (response){
            if(response=="no") {
                alert('Sorry we couldnt find the location');
                $('input[name="your-address"]').val('');
            } else {
                //$(".wpcf7-submit").unbind("submit").submit(); 
                //$(".wpcf7-submit").unbind("submit");
            }
        });
    }
});

我没有检查它,因为我没有其他代码片段(gs_cf7_check_lat_lag),所以请将其视为如何制定解决方法的想法。

相关问题