试图阻止页面在刷新时重新提交表单

时间:2012-02-18 07:19:01

标签: forms wordpress header refresh contact

我有以下代码。我试图阻止我的页面在页面刷新后使用标题重新提交表单(“location:location.php”);但是我在下面列出了错误

错误

Warning: Cannot modify header information - headers already sent by (output started at /Users/anderskitson/Sites/fiftyfity/wp-content/themes/fiftyfityNew/contact-form copy.php:7) in /Users/anderskitson/Sites/fiftyfity/wp-content/themes/fiftyfityNew/contact-form copy.php on line 68

代码

<?php
/*
Template Name: form-test.php
*/
?>

<?php include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); ?>
<?  include_once(ABSPATH. 'wp-content/themes/fiftyfityNew/contact-form.php'); ?>


<?php
/**
 * The template for displaying all pages.
 *
 * This is the template that displays all pages by default.
 * Please note that this is the wordpress construct of pages
 * and that other 'pages' on your wordpress site will use a
 * different template.
 *
 * @package WordPress
 * @subpackage Starkers
 * @since Starkers 3.0
 */

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(); ?>

1 个答案:

答案 0 :(得分:0)

问题是,您所包含的其中一个脚本在WordPress有机会之前发送标头,或者include_onceget_header语句之间的空行使Web服务器本身发送默认标题。

因此,修改前几行(即删除额外的PHP开启/关闭标记):

<?php
/*
Template Name: form-test.php
*/

include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
include_once(ABSPATH. 'wp-content/themes/fiftyfityNew/contact-form.php');

/**
 * The template for displaying all pages.
 *
 * This is the template that displays all pages by default.
 * Please note that this is the wordpress construct of pages
 * and that other 'pages' on your wordpress site will use a
 * different template.
 *
 * @package WordPress
 * @subpackage Starkers
 * @since Starkers 3.0
 */

get_header(); ?>

同时检查所包含的文件是否有任何数据输出,header()来电或类似的相邻开/关标签。