1

I have created two pages – Contribute Page & Checkout Page.

On Contribute Page I have following code:

<?php
if(isset($_POST['submitnow'])) {
        global $wpdb;
$wpdb->insert( 
    'wp9c_treetweet', 
    array( 
        'name' => $_POST['aname'], 
        'amt' => $_POST['aamt'],
        'dept' => $_POST['adept']
    ), 
    array( 
        '%s', 
        '%d',
                '%s' 
    )

);        
}    
?>

<form action="" method="POST" id="addcourse">
<label>Name:<input type="text" name="aname" size="30" required></label>
<label>Amount:<input type="text" name="aamt" size="30" required></label>
<label>Department:<input type="text" name="adept" size="30" required></label>

<input type="submit" name="submitnow" id="addcoursesubmit" value="Submit">
</form>

This code is working fine and is inserting the form data properly in database.

To redirect this page to Checkout Page I have added following code in functions.php:

function redirect_from_checkout() {

   if( !isset( $_POST['submitnow'] ) )

       return;

         wp_redirect( home_url( '/checkout/' ) );

         exit;  
}

add_action( 'template_redirect', 'redirect_from_checkout' );

After adding redirect code in functions.php the redirection is working properly but the form data is not inserting into database table.

Plz help me out with a solution.

1

According to WordPress Codex about the template_redirect hook:

This action hook executes just before WordPress determines which template page to load.

So, you are redirecting the user to another page before the template is even loaded, which your form won't load at all. You can use another hook which runs after template_redirect, such as wp_footer.

You can also redirect the user right after inserting the form in the database, by changing your code to:

<?php
    if(isset($_POST['submitnow'])) {
        global $wpdb;
        $wpdb->insert( 
            'wp9c_treetweet', 
            array( 
                'name' => $_POST['aname'], 
                'amt' => $_POST['aamt'],
                'dept' => $_POST['adept']
            ), 
            array( 
                '%s', 
                '%d',
                        '%s' 
            )

        );
        wp_safe_redirect(home_url( '/checkout/' ));
        exit();
    }
?>

This way, if the form is inserted, the user will be redirected afterward.

6
  • Nope its not working.
    – Minu
    Jun 10 '17 at 15:56
  • Why not simply redirect the user after you insert the form into the database? You can add wp_safe_redirect() right after $wpdb->insert (...).
    – Jack Johansson
    Jun 10 '17 at 15:58
  • Thanks for ur valuable answers but can u plz modify my code. It will be a great help for me.
    – Minu
    Jun 10 '17 at 16:06
  • @Minu I updated my answer for you.
    – Jack Johansson
    Jun 10 '17 at 16:26
  • actually the more correct answer is not to submit to the same url as the page being displayed, as this also make sure you can refresh the page after form submission without annoying messages from the browser Jun 10 '17 at 20:37

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.