在联系表单 7 (CF7) 中发送 HTML 值

问题描述 投票:0回答:2

目前我有一个页面,其中有一些 JS 按钮和按钮下方的 CF7 联系表单

我在此页面上有一个字段,显示由按钮设置的值

<input type="number" id="price"/>

我想知道是否有一种简单的方法可以使用类似

document.getElementById('price').value

的内容发送 CF7 电子邮件中的价格值

希望得到一些帮助!

javascript wordpress contact-form-7
2个回答
0
投票

您可以在 cf7 中创建价格隐藏字段:

// hidden was css class for hidding field, id is your input price id for cf7
[number price class:hidden  id:price_to_send]

然后将 cf7 之外的输入价格的值添加到隐藏字段中

$('#price').change(function() {
    $('#price_to_send').val($(this).val());
});

注意:如果您需要在发送之前验证输入,您可以使用 wpcf7_before_send_mail 操作,或其他操作/过滤器:http://hookr.io/plugins/contact-form-7/4.7/


0
投票
add_action( 'wpcf7_before_send_mail', 'change_my_data', 10, 1 );
function change_my_data($instance) {
    if( $instance->id() == "266" ) {
        $submission = WPCF7_Submission::get_instance();
        if ( $submission ) {
            $posted_data = $submission->get_posted_data();
            $properties = $instance->get_properties();
            $body = $properties['mail']['body'];
            $body = str_replace('[price]', html_entity_decode($posted_data['price']), $body);
            $properties['mail']['body'] = $body;
            $instance->set_properties($properties);
        }
    }
    return $instance;
}
© www.soinside.com 2019 - 2024. All rights reserved.