• wpreceiver

    (@wpreceiver)


    Hi,

    I have a Contact Form 7 filter function to modify the form. Based on the user inputs I am differentiating as eligible and non-eligible. Then I want to redirect it to respective thank you pages. There are two thank you pages. One for eligiable and other one for not eligible. So, at the end I used this code to call to a function to do this.

    add_filter('wpcf7_before_send_mail', 'validate_and_redirect');
    function validate_and_redirect($contact_form) {
            // my form codes
            cf7_eligibility_thank_you_redirect($eligibility);
    }
    function cf7_eligibility_thank_you_redirect($eligibility) {
    ?>
    <script type="text/javascript">
                document.addEventListener( 'wpcf7mailsent', function( event ) {
                    // Check the eligibility value
                    if ('<?php echo $eligibility; ?>' === 'Eligible') {
                        location = 'https://mywebsite.com/thank-you-eligible/';
                    } else {
                        location = 'https://mywebsite.com/thank-you-not-eligible/';
                    }
                }, false );
            </script>
    <?php
    }

    When I run the code, I am getting this error. Form is submitting and receiving. But this redirection is not working. And spinning all along. Once I remove the function call from the filter function all works well.

    {code: 'invalid_json', message: 'The response is not a valid JSON response.'}

    Can you let me know how can I achieve this and do the redirection?

    Note: Tested with disabling all plugins except CF7 and switching the theme to a default theme. Getting the same error.

    • This topic was modified 11 months ago by wpreceiver.
  • The topic ‘How to call to a function at the end of the filter?’ is closed to new replies.