Php 联系人表单7提交功能不起作用

Php 联系人表单7提交功能不起作用,php,wordpress,function,plugins,contact-form-7,Php,Wordpress,Function,Plugins,Contact Form 7,我正在Wordpress中使用Contact Form 7,我想做的是在我创建的表单提交成功时调用一个特定的JavaScript函数。这是我的密码 add_action('wpcf7_mail_sent', function ($cf7) { // Run code after the email has been sent $wpcf = WPCF7_ContactForm::get_current(); $wpccfid=$wpcf->id; // if yo

我正在Wordpress中使用Contact Form 7,我想做的是在我创建的表单提交成功时调用一个特定的JavaScript函数。这是我的密码

    add_action('wpcf7_mail_sent', function ($cf7) {
    // Run code after the email has been sent
 $wpcf = WPCF7_ContactForm::get_current();
$wpccfid=$wpcf->id;
    // if you wanna check the ID of the Form $wpcf->id
     if ( '3854' == $wpccfid ) { // Change 123 to the ID of the form 
echo '
 <div class="modal" id="myModal2" role="dialog" style="display:block;" tabindex="-1">
    <div class="modal-dialog">
      <!-- Modal content-->
      <div class="modal-content no_pad text-center">
         <button type="button" class="close" data-dismiss="modal">&times;</button>
        <div class="modal-header heading">
          <h3 class="modal-title">Message Sent!</b></h3>
        </div>
        <div class="modal-body">

            <div class="thanku_outer define_float text-center">
                <h3>Thank you for getting in touch!</h3>
            </div>
        </div>
        <div class="modal-footer">
        </div>
      </div>

    </div>
    </div>
';
    }
});
add_action('wpcf7_mail_sent')函数($cf7){
//发送电子邮件后运行代码
$wpcf=WPCF7_ContactForm::get_current();
$wpccfid=$wpcf->id;
//如果您想检查表单$wpcf->ID的ID
if('3854'=$wpcfid){//将123更改为表单的ID
回声'
&时代;
消息已发送!
谢谢你的联系!
';
}
});
我有一个错误,我不知道如何修复它


在Ajax中调用
wpcf7\u mail\u sent
函数,系统希望Ajax回复为
JSON
。因此,当您直接
echo
一些HTML时,会破坏JSON,Javascript无法解析它

我找不到任何可以使用自定义HTML代码的
wpcf7\u mail\u sent
示例,因此可能无法这样做。问题是代码返回一个
消息
JSON变量,我认为这应该是纯文本,而不是HTML

备选方案1:在Javascript中编辑DOM

我看到的另一种方法是使用Javascript而不是PHP,在
wpcf7mailsent
事件上使用侦听器

见下例:

<script>
    document.addEventListener( 'wpcf7mailsent', function( event ) {
            // Your code to edit the HTML goes here
    }, false ); 
</script>
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
    if ( 'FORMID' === event.detail.contactFormId ) {
        location = 'REDIRECTURL';
    }
}, false );
</script>