• Hi,

    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.

    • This topic was modified 7 years, 5 months ago by Mineshrai.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Try adding the redirect after inserting the data

    
    <?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_redirect( home_url( '/checkout/' ) );
    exit;	
    }    
    ?>
    
    Thread Starter Mineshrai

    (@mineshrai)

    @omarkasem

    Hi Thanks for ur Answer but the code is not working.

    Moderator bcworkz

    (@bcworkz)

    Omar’s code looks fine, it should work. Where does this code occur on your template? Redirects must be called before any output. If there is a call to get_header() that occurs before, or anything else that can cause output (like if this template is included or loaded by another template), the redirect will fail. This code must be pretty much the first thing to execute when the main template is loaded.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Form Data not getting stored in database’ is closed to new replies.