Php 在web表单上保存信息提交时出错

Php 在web表单上保存信息提交时出错,php,html,forms,validation,Php,Html,Forms,Validation,我有一个php脚本,我正在工作,还没有能够得到表单来保存信息和重新填充验证错误。我在不同的网站上尝试了几个how-to,但可能我遗漏了一些东西,因为它们大多有很多其他PHP行,但是how-to页面没有给出保存数据和重新填充所需的代码行的细节。下面是代码,如果有人有任何想法,或者可以给我指出一个工作良好的地方,我将不胜感激,到目前为止,我遇到了麻烦。提前谢谢 <?php /* Template Name: Post Submit Form */ ?&g

我有一个php脚本,我正在工作,还没有能够得到表单来保存信息和重新填充验证错误。我在不同的网站上尝试了几个how-to,但可能我遗漏了一些东西,因为它们大多有很多其他PHP行,但是how-to页面没有给出保存数据和重新填充所需的代码行的细节。下面是代码,如果有人有任何想法,或者可以给我指出一个工作良好的地方,我将不胜感激,到目前为止,我遇到了麻烦。提前谢谢

    <?php
    /*
    Template Name: Post Submit Form
    */
    ?>
    <?php if ( $user_ID > 0) {  ?>
    <?php if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {
        // Do some minor form validation to make sure there is content
        if (isset($_POST['submit'])) {
                $error = "";

            if (!empty($_POST['title'])) {
                $title = $_POST['title'];
         } else {
            $error .= "Please add a title<br />";
        }

            if (!empty($_POST['description'])) {
                $description = $_POST['description'];
         } else {
            $error .= "Please add a description<br />";
        }

            if (!empty($_POST['post_tags'])) {
                $post_tags = $_POST['post_tags'];
         } else {
            $error .= "Please add some keywords<br />";
        }
            if (!empty($_POST['externalurl'])) {
                $externalurl = $_POST['externalurl'];
         } else {
            $error .= "Please add a URL to post<br />";
        }
            // IMAGE VALIDATION - CHECK IF THERE IS AN IMAGE AND THAT ITS THE RIGHT FILE TYPE AND RIGHT SIZE
            if ($_FILES) {
                foreach ($_FILES as $file => $array) {
                    //Check if the $_FILES is set and if the size is > 0 (if =0 it's empty)

                    if(isset($_FILES[$file]) && ($_FILES[$file]['size'] > 0)) {

                        $tmpName = $_FILES[$file]['tmp_name'];
                        list($width, $height, $type, $attr) = getimagesize($tmpName);

                    if($width<=899 || $height<=299)
                    {
                        $error .= "Image is to small. Minimum 900 pixels wide.<br />";
                        unlink($_FILES[$file]['tmp_name']); 
                    }

                    // Get the type of the uploaded file. This is returned as "type/extension"
                    $arr_file_type = wp_check_filetype(basename($_FILES[$file]['name']));
                    $uploaded_file_type = $arr_file_type['type'];

                     // Set an array containing a list of acceptable formats
                    $allowed_file_types = array('image/jpg','image/jpeg');

                     // If the uploaded file is the right format
                    if(in_array($uploaded_file_type, $allowed_file_types)) {

                    } else { // wrong file type
                    $error .= "Please upload a .jpg type image<br />";
                         }

                    } else {
                    $error .= "Please add an image<br />";
                    }
                } // end for each
            } // end if

            $tags = $_POST['post_tags'];
            $externalurl = $_POST['externalurl'];

            // ADD THE FORM INPUT TO $new_post ARRAY
            if (empty($error)) {
                $new_post = array(
                'post_title'    =>  $title,
                'post_content'  =>  $description,
                'post_category' =>  array($_POST['cat']),  // Usable for custom taxonomies too
                'tags_input'    =>  array($tags),
                'post_status'   =>  'preview',           // Choose: publish, preview, future, draft, etc.
                'post_type' =>  'post',  //'post',page' or use a custom post type if you want to
                'externalurl'   =>  $externalurl
            );

            //SAVE THE POST
            $pid = wp_insert_post($new_post);

            //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
            wp_set_post_tags($pid, $_POST['post_tags']);

            //REDIRECT TO THE NEW POST ON SAVE
            $link = get_permalink( $pid );
            wp_redirect( $link );

            //ADD OUR CUSTOM FIELDS 
            add_post_meta($pid, 'externalurl', $externalurl, true); 

                //INSERT OUR MEDIA ATTACHMENTS
                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, $pid );
                }   
            }
            if ($attach_id > 0){
                //and if you want to set that image as Post  then use:
                update_post_meta($pid,'_thumbnail_id',$attach_id);
            }
            } // END SAVING POST
        } // END VALIDATION
    } // END THE IF STATEMENT THAT STARTED THE WHOLE FORM

    //POST THE POST YO
    do_action('wp_insert_post', 'wp_insert_post');

    ?>


    <?php get_header(); ?>
    <?php get_template_part('wrapper', 'start'); ?>


            <article id="post-<?php the_ID(); ?>" <?php post_class('box mb20'); ?>>


    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

                    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                        <?php if ( is_front_page() ) { ?>
                            <h2 class="entry-title"><?php the_title(); ?></h2>
                        <?php } else { ?>
                            <h1 class="entry-title"><?php the_title(); ?></h1>
                        <?php } ?>

                        <div class="form-content">
                         <?php
                            if (!empty($error)) {
                                echo '<p class="error"><strong>Your message was NOT sent<br/> The following error(s) returned:</strong><br/>' . $error . '</p>';
                            } elseif (!empty($success)) {
                                echo '<p class="success">' . $success . '</p>';
                            }
                        ?>
                            <div class="entry-content clearfix mt20">
                                <?php the_content(); ?>
                            </div>
                            <div class="dashedline"></div>

            <!-- Start FORM -->

            <div class="submitpost-form">
            <form id="new_post" name="new_post" method="post" action="" class="submitpost-form" enctype="multipart/form-data">
                <!-- post name -->
                <fieldset name="name">
                    <label for="title">Post Title:</label>
                    <input type="text" id="submitpost-entry" value="<?php echo $_SERVER['title']; ?>" tabindex="5" name="title" />
                     <p class="submitpost-entry-infotext">Choose a descriptive title that highlights the most important thing about your project. This will also be part of the URL. Do not use your name or genre in it.</p>
                </fieldset>

                <!-- External URL -->
                <fieldset class="externalurl">
                    <label for="externalurl">Direct link (URL) to post page: (must include http://)</label>
                    <input type="text" value="<?php echo $_SERVER['externalurl']; ?>" id="submitpost-entry" tabindex="10" name="externalurl" onFocus="this.value=''"/>
                    <p class="submitpost-entry-infotext">This will be the URL linked to by the post. This should be a direct url to the webpage / article about the project. Do not link to your homepage or main portfolio page. The image submited below must appear on this page.</p>
                </fieldset>

                <!-- post tags -->
                <fieldset class="tagsentry">
                    <label for="post_tags">Tags (comma separated):</label>
                    <input type="text" value="" tabindex="15" name="post_tags" id="submitpost-entry" />
                    <p class="submitpost-entry-infotext">Use a few descriptive words (all lowercase) to allow users to discover your work via exploring tags. Avoid using genre terms. Example: environmental, b&w, lifestyle. Keep below about ten.</p>
                </fieldset>


                <!-- post Category -->
                <fieldset class="category">
                    <label for="cat">Genre:</label>
                    <?php wp_dropdown_categories( 'tab_index=20&taxonomy=category&hide_empty=0&exclude=1' ); ?>
                    <p class="submitpost-entry-infotext" style="position:relative; top:-14px;">Select the most suitable genre for your work. If you have a genre suggestion please let us know via the contact page.</p>
                </fieldset>

                <!-- authors -->
                <fieldset class="images">
                    <label for="bottle_front">Image</label>
                    <input type="file" name="Image" id="image" tabindex="25" />
                    <p class="submitpost-entry-infotext">Images must be .jpg format with a minimum of 900 pixels wide and be less than 600kb in size.</p>
                </fieldset>

                <!-- post Content -->
                <fieldset class="submitpostcontent">
                    <label for="description">Description:</label>
                    <textarea id="submitpost-message" tabindex="30" name="description" rows="10"></textarea>
                    <p class="submitpost-entry-infotext" >Enter more detail about the project or piece, the first 40 words will be displayed on the grid page. Any extra text will show on the post page. Do not compose in this field to avoid loosing text. Spell check before submiting! Profanity or offensive text will not be published. </p>
                </fieldset>


                <fieldset class="submit">
                    <input type="submit" value="Post Review" tabindex="40" id="submit" name="submit" />
                </fieldset>

                <input type="hidden" name="action" value="new_post" />
                <?php wp_nonce_field( 'new-post' ); ?>
            </form>


            </div> <!-- END WPCF7 -->

            <!-- END OF FORM -->
                            <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
                            <?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
                        </div><!-- .entry-content -->
                    </div><!-- #post-## -->

                    <?php comments_template( '', true ); ?>

    <?php endwhile; // end of the loop. ?>

        </article>

    <?php get_template_part('wrapper', 'end'); ?>


    <?php get_footer(); ?>



    <?php } else { ?>


    <?php get_header(); ?>

    <?php get_template_part('wrapper', 'start'); ?>

        <?php while (have_posts()) : the_post(); ?>

    <article id="post-<?php the_ID(); ?>" <?php post_class('box mb20'); ?>>

        <header class="entry-header">
            <h1 class="entry-title"><?php the_title(); ?></h1>
        </header>

            <div class="entry-content clearfix mt20">
                Message for non logged in users.
            </div>



    </article>  

            <?php comments_template(); ?>

        <?php endwhile; ?>

    <?php get_template_part('wrapper', 'end'); ?>

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

形象

图像必须是.jpg格式,最小宽度为900像素,大小小于600kb

说明:

输入有关项目或作品的更多详细信息,前40个单词将显示在网格页面上。任何额外的文本都将显示在帖子页面上。请勿在此字段中撰写,以免丢失文本。提交前请检查拼写!亵渎或冒犯性的文字将不会发表


这就是它的工作原理:

<form id="new_post" name="new_post" method="post" action="this_form.php" class="submitpost-form" enctype="multipart/form-data">

<input type="text" id="submitpost-entry" value="<?php echo $_POST['title']; ?>" tabindex="5" name="title" />


为了避免未定义的索引错误,您应该使用以下内容:

<form>
    <input type="text" id="someField" name="title" value="<?php echo !empty( $_POST['title']  ) ? $_POST['title'] : ''?>" />
</form>


我不确定我是否明白你的要求。如果由于验证错误而重新显示表单,则要预填充表单字段的值,则可以在
$\u POST
中访问它们-只需设置输入的值属性。您可以添加一个
isset
检查,以确保它们也已设置好。好吧,@art2成功了!我正在查找使用$\u服务器('title')的东西。我现在唯一不能得到的是,当使用相同的值条目时,输入类型文件不会重新加载数据。对于任何在textarea中看到这一点的人,我必须为文件输入类型执行
@Brian York,您需要使用不同的方法来“填充”字段,因为据我所知,这是不可能的,您无法将POST数据返回到文件输入,因为它没有这样的属性。例如,您可以在复选框列表中显示所有上载的文件。对不起,类型应为“文本”,而不是“测试”。我已更新了答案。(当然,除非这不是它给出的错误)。这是在做什么,art2没有做的?哦,我忘了关闭PHP标签。更新。