【问题标题】:Getting errors after form submit - Wordpress表单提交后出现错误 - Wordpress
【发布时间】:2014-08-14 22:24:36
【问题描述】:

我有一个简单的表单,可以添加新帖子并设置特色图片。我正在尝试使用以下方法清除表单:header("Location: $_SERVER[PHP_SELF]"); 但在页面重新加载后我收到“标头已发送”错误消息和“未定义索引”。

Notice: Undefined index: attach in /var/www/vhosts/smoige.com/jobs/wp-content/themes/twentyfourteen/page-templates/upload.php on line 20 Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/smoige.com/jobs/wp-includes/post-template.php:478) in /var/www/vhosts/smoige.com/jobs/wp-content/themes/twentyfourteen/page-templates/upload.php on line 53

 <?php
/**
 * Template Name: Submit
 *
 * @package WordPress
 * @subpackage Twenty_Fourteen
 * @since Twenty Fourteen 1.0
 */
get_header(); ?>

   <div id="nav-submit-form" style="margin: 0 0 0 333px;">
<span class="nav-submit-form">
    <?php 
    if ( is_user_logged_in() ) {

    $current_user = wp_get_current_user();

      if(isset($_POST['submit'])) {

              $filename = $_FILES['attach']['name'];

              $new_post = array(
                  'ID' => '',
                  'post_author' => $current_user->ID, 
                  //'post_category' => array(3322),
            'tags_input' => "",
                  'post_title' => wp_strip_all_tags( $_POST['post_title'] ),
                  'post_status' => 'publish'
                );

                $post_id = wp_insert_post($new_post);
                $post = get_post($post_id);
                $new_post = $post->ID;

             if (!function_exists('wp_generate_attachment_metadata')) {
                    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
                    require_once(ABSPATH . "wp-admin" . '/includes/media.php');
             }
                     if ($_FILES) {
                          foreach ($_FILES as $file => $array) {
                              if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                                  return "upload error : " . $_FILES[$file]['error'];
                              }
                              $attach_id = media_handle_upload( $file, $new_post );
                          }

                    }
                    if ($attach_id > 0){
                        //and if you want to set that image as Post  then use:
                        update_post_meta($new_post,'_thumbnail_id',$attach_id);
                    }
              header("Location: $_SERVER[PHP_SELF]");
        }

?>
      <form method="post" enctype="multipart/form-data" action="" id="upload-form">
          <input type="text" name="post_title" value="Image Name" size="45" id="input-title"/>
          <input type="file" name="attach" id="image" />
          <input id="submitButton" class="subput" type="submit" name="submit" value="Add"/>
      </form>

<?php } else {
        echo 'Welcome, visitor!';
    }

?>
</span>
 </div><!-- #nav-submit-form -->

<?php get_sidebar(); get_footer(); ?>

【问题讨论】:

    标签: php wordpress forms


    【解决方案1】:
    $filename = $_POST['attach']; 
    

    应该是

    $filename = $_FILES['attach']['name'];
    

    http://www.php.net//manual/en/features.file-upload.post-method.php

    【讨论】:

      【解决方案2】:

      标头已发送:

      对于“标头已发送”错误,如果任何输入已发送到浏览器(HTML 标记、带有 header() 的内容、文件开头的空格),则无法修改标头...

      所以,假设您使用 echo('hello there') 向用户输出一些内容。然后,尝试使用 header('Location: ...') 重定向用户,它不起作用。

      函数头必须修改HTTP头来重定向用户。

      “echo”将单词“hello there”与 HTTP 标头一起发送给用户。如果没有 HTTP 标头,则服务器无法将其留给浏览器,因此 PHP 只添加了一个。

      之后,您尝试使用 header('Location'); 对其进行修改。好吧,PHP 会告诉你“很抱歉,但你要求我修改标题,而我已经为页面发送了它......太糟糕了”。

      上周我写了一篇关于它的文章:http://www.mogosselin.com/warning-cannot-modify-header-information-headers-already-sent-by/如果您需要更多信息。

      要找出问题出在哪里,关于已经发送的 Header

      检查错误信息

      已发送的标头(输出开始于 /var/www/vhosts/smoige.com/jobs/wp-includes/post-template.php:478) 在 /var/www/vhosts/smoige.com/jobs/wp-content/themes/twentyfourteen/page-templates/upload.php 在第 53 行

      脚本 post-template.php @ line 478 中的某些内容尝试输出某些内容,但标头已发送。

      标头由 upload.php 中的第 53 行发送。

      未定义索引:

      对于未定义的索引,这意味着您尝试在数组中使用的 INDEX 不存在。例如: $_POST['NAME']; NAME 是索引。

      如果你试试这个: $var = $_POST['FDASFSDA']; 你会得到一个“未定义的索引”,因为 FDASFSDA 没有在 $_POST 中设置

      【讨论】:

      • 这会导致错误吗? get_header(); 我在问题中发布的代码上方有它。
      • 我认为 get_header() 是一个 wordpress 函数:codex.wordpress.org/Function_Reference/get_header 这包括一个主题的标题。这与 HTTP 标头不同。还是您的意思是 get_headers() (带有 S)?
      • 没有。 wordpress 函数 get_header() 就是我在代码上方所拥有的。我刚刚添加了所有代码......你能看到可以改变的东西吗?
      • 我在帖子中添加了精确度。检查错误消息以找出输出完成的位置(将标头发送到浏览器的内容)以及在 PHP 发送标头后尝试更改标头的内容。
      【解决方案3】:

      我使用了你的代码,这适用于在文件开头添加这一行(可能你的路径不同):

      include("../../../wp-load.php");
      

      完整代码:

      <?php
      include("../../../wp-load.php");
      
      if ( is_user_logged_in() ) {
      
          $current_user = wp_get_current_user();
      
            if(isset($_POST['submit'])) {
      
                    //$filename = $_POST['attach'];
                    $filename = '';
                    if(isset($_FILES['attach'])) {
                                      $filename = $_FILES['attach']['name'];
                                  }
      
                    $new_post = array(
                        'ID' => '',
                        'post_author' => $current_user->ID,
                        //'post_category' => array(3322),
                  'tags_input' => "",
                        'post_title' => wp_strip_all_tags( $_POST['post_title'] ),
                        'post_status' => 'publish'
                      );
      
                      $post_id = wp_insert_post($new_post);
                      $post = get_post($post_id);
                      $new_post = $post->ID;
      
                   if (!function_exists('wp_generate_attachment_metadata')) {
                          require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                          require_once(ABSPATH . "wp-admin" . '/includes/file.php');
                          require_once(ABSPATH . "wp-admin" . '/includes/media.php');
                   }
                           if ($_FILES) {
                                foreach ($_FILES as $file => $array) {
                                    if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                                        return "upload error : " . $_FILES[$file]['error'];
                                    }
                                    $attach_id = media_handle_upload( $file, $new_post );
                                }
      
                          }
                          if ($attach_id > 0){
                              //and if you want to set that image as Post  then use:
                              update_post_meta($new_post,'_thumbnail_id',$attach_id);
                          }
                    header("Location: $_SERVER[PHP_SELF]");
              }
      
      ?>
            <form method="post" enctype="multipart/form-data" action="" id="upload-form">
                <input type="text" name="post_title" value="Image Name" size="45" id="input-title"/>
                <input type="file" name="attach" id="image" />
                <input id="submitButton" class="subput" type="submit" name="submit" value="Add"/>
            </form>
      
      <?php } else {
              echo 'Welcome, visitor!';
          }
      
      ?>
      

      享受您的代码吧! :)

      【讨论】:

      • 我觉得很奇怪,你的答案和我的答案在没有任何“评论”或解释的情况下被否决。 :|
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      • 2014-02-25
      • 2018-11-24
      • 2017-08-24
      相关资源
      最近更新 更多