试图阻止刷新页面重新提交表格
我有以下代码。我试图阻止我的页面在页面刷新后使用标题(“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(); ?>
的问题是要包括脚本的一个发送头WordPress的得到一个机会之前这样做,或者您的include_once
和get_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()
来电或类似的相邻开/结束标签。
我已经尝试这样做,我仍然得到同样的错误。 – 2012-02-18 22:00:41
然后检查包含输出语句的文件(以及任何递归包含的文件),内联HTML数据(包括空格或PHP打开/关闭标记)以及“header()”调用。这些都会阻止WordPress发送它自己的头文件。您可以通过暂时删除'include_once'语句来确认。 – Simon 2012-02-19 11:10:04
您很可能需要修改您的脚本,包括以符合WordPress的方式发送其头文件。在这种情况下,请查看'wp_headers'和'send_headers'过滤器[here](http://adambrown.info/p/wp_hooks/hook/send_headers?version=3.3&file=wp-includes/class-wp.php) – Simon 2012-02-19 11:12:51
http://*.com/questions/2938777/headers-already-sent的可能重复 – 2012-02-18 08:42:16