尝试阻止页面在刷新时重新提交表单
我有以下代码。我试图阻止我的页面在使用 header("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(); ?>
I have the following code. I am trying to prevent my page from resubmitting a form after a page refresh with the header("location:location.php"); However I get a error Listed below
Error
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
Code
<?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(); ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于,您所包含的脚本之一在 WordPress 有机会发送标头之前就发送了标头,或者
include_once
和get_header
语句之间的空行使得Web 服务器本身发送其默认标头。因此,将前几行修改为如下所示(即删除额外的 PHP 开始/结束标记):
还要检查包含的文件中是否有任何数据输出、
header()
调用或类似的相邻开始/结束标记。The problem is that one of the scripts you are including sends headers before WordPress gets a chance to do so, or that the blank lines between your
include_once
and theget_header
statement makes the web server itself send its default headers.So, modify the first few lines to look like (i.e. removing the extra PHP opening/closing tags):
Also check the included files for any data output,
header()
calls or similar adjacent opening/closing tags.