0
votes

Im trying to insert some jQuery into a gravity forms hook (gform_post_paging) to disable the previous button on a multi-page form. I am getting a jQuery is not defined error. I am sure jQuery is being called so not sure whats going on. The multipage form is using ajax if that matters.

my code:

add_action( 'gform_post_paging_4', 'alert_user', 10, 3 );
function alert_user( $form, $source_page_number, $current_page_number ) {
        if ( $current_page_number == 2 ) {

            $to = $_POST['input_3'];
            $subject = ot_get_option( 'consultant_verification_email_subject' );
            $headers = array('Content-Type: text/html; charset=UTF-8');
            // get the email content from option tree.  We are setting a static value for the code.
            // if this code needs to change, it should be updated in gravity forms for now
            // @todo: we could randomize this value if we want then check the value using jquery
            $body = ot_get_option( 'consultant_verification_email' );

            wp_mail( $to, $subject, $body, $headers );

            ?>

            <script type="text/javascript">
                alert( 'An email was sent to <?php echo $to; ?> with your verification code.  Please enter this code to continue.  Should you have failed to receive this email, please click the previous button, confirm your email, and click next again.  Should you continue to encounter difficulty, please contact us.' );
            </script>

            <?php
        } else if ( $current_page_number == 3 ) {
            // disable the previous button so user cant go through verification steps again
            ?>
            <script type="text/javascript">
                jQuery(".gform_previous_button").prop("disabled",true);
            </script>
            <?
        }
    }
1
you are calling jquery before its loaded. Place the script into a js file that is enqueued (google wp_enqueue_script). - David
What if i only want the script to run when a certain page number is loaded (see conditional statements in code above)? - Koleon
same logic but ill update a vanilla js function below which you can just use in place. - David

1 Answers

0
votes

Probably just as easy to use vanilla js for this:

    <script type="text/javascript">
        document.getElementsByClassName('gform_previous_button')[0].disabled = true;
    </script>

Just as easy!