• I have a contact form that each time I refresh the page I get a prompt to resend all the data, I have added a header(“location: location.php”); to the bottom of the form function to try and prevent this. However all I get is a Cannot Modify Header Information – Headers already sent error. Here is my code

    <?php
    /*
    Template Name: form-test.php
    */
    include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
    include_once(ABSPATH. 'wp-content/themes/fiftyfityNew/contact-form.php');
    get_header(); ?>
    
    <script>
    var i=0;
    function test(){
    
    for(i=0;i<=5;i++){
    	document.write( "the number is" + i);
    }
    }
    </script>
    <script>
    test();
    </script>
    
    <?php function make_user_feedback_form() {
        global $wpdb;
        global $current_user;
    
            $ufUserID = $current_user->ID;
    
            if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'updateFeedback' ) {
                $ufDataUpdate = $wpdb->insert( 'wp_user_feedback', array( 'date' => current_time('mysql'), 'responses' => $_POST["test"]) );
            }
            }?>
        <div id="form">
        <ol>
            <form method="post">
                <li><label for="test">Pick a date Babe:</label><input type="text" id="datepicker" name="test" value="" /></li> <!-- the (name="test") value is what the ('responses' => $_POST["test"]) value is talking too -->
    
                <li><input name="submit" type="submit" id="submit" class="submit button" value="Send feedback" /></li>
                <?php wp_nonce_field( 'updateFeedback' ); ?>
                <input name="action" type="hidden" id="action" value="updateFeedback" />
            </form>
        </ol>
        </div>
        <?php 
    
    add_action('the_content','make_user_feedback_form');
    
    header("location: http://localhost:8888/fiftyfity/?page_id=90");
    
    ?>
    
    <?php
    make_user_feedback_form();
    ?>
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
Viewing 1 replies (of 1 total)
  • That’s right. You can’t modify headers after they’ve been sent. See:

    http://php.net/manual/en/function.header.php

    Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

    The only place your code would work would be above the DOCTYPE declaration, and only then if nothing else had been sent by any of the other files that WordPress includes before any code you get to see in your theme.

    Also, see the forum rules about posting code.

    Cheers

    PAE

Viewing 1 replies (of 1 total)

The topic ‘Cannot modify header’ is closed to new replies.