1

I have a front-end form and I'm using a POST handler in a plugin using the init hook. Basically, it's:

<form action="" method="POST" id="mktinto-form" class="profile-form">
  <!-- form fields here -->
  <input type="hidden" name="action" value="profile-form">
  <input type="submit" value="Submit">
</form>

And the handler:

function vv_process_profile_forms() {
  if ( isset( $_POST['action'] ) && $_POST['action'] == 'profile-form' ) {

    // form processing code here

    $redirect_page = get_permalink( 586 );
    wp_redirect( $redirect_page ); exit;
  }
}
add_action( 'init','vv_process_profile_forms' );

The form is submitted and the page redirects to the next page in the process.

The form posts fine, the redirect happens, but the form handler actually runs twice. If I remove the redirect, it only runs once, like it should, but then, of course, it doesn't move to the next page. So it seems like the double posting is a result of the redirect, but I have no idea why.

0

You can fix it through Session. try

function vv_process_profile_forms() {
    session_start();

    if ( isset( $_POST['action'] ) && $_POST['action'] == 'profile-form' ) {

        if ( ! isset( $_SESSION['form-submit'] ) ) {

            // form processing code here

            $_SESSION['form-submit'] = 1;

            $redirect_page = get_permalink( 586 );
            wp_redirect( $redirect_page );
            exit;

        } else {

            unset( $_SESSION['form-submit'] );
        }
    }
}
add_action( 'init','vv_process_profile_forms' );
0

I meant to answer this earlier. Turns out the problem was developer error (me!). I was using a javascript form handler that would process data and pass it on to the php form handler. Turns out that I was actually loading the .js file twice on the page. Kicked myself real hard for wasting the better part of a day figuring that out.

I had actually fixed it by unbinding the form submit event in the javascript, but then I found the root cause and no longer needed to do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.