2012-02-18 189 views
0

我有以下代碼。我試圖阻止我的頁面在頁面刷新後使用標題(「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(); ?> 
+0

http://stackoverflow.com/questions/2938777/headers-already-sent的可能重複 – 2012-02-18 08:42:16

回答

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()來電或類似的相鄰開/結束標籤。

+0

我已經嘗試這樣做,我仍然得到同樣的錯誤。 – 2012-02-18 22:00:41

+0

然後檢查包含輸出語句的文件(以及任何遞歸包含的文件),內聯HTML數據(包括空格或PHP打開/關閉標記)以及「header()」調用。這些都會阻止WordPress發送它自己的頭文件。您可以通過暫時刪除'include_once'語句來確認。 – Simon 2012-02-19 11:10:04

+0

您很可能需要修改您的腳本,包括以符合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