【问题标题】:Form data and image preview after form submit表单提交后的表单数据和图像预览
【发布时间】:2013-10-13 21:01:52
【问题描述】:

我使用 wordpress cms,我需要定制表单方面的帮助。它允许用户从前端发布。有几个字段和一个图像上传字段。这是一个由两部分组成的表单,第一部分是他提交数据的地方,第二部分是他刚刚提交的数据的预览。请记住,这发生在提交之后

表格代码:

<?php
    global $wpdb;
    $this_page  =   $_SERVER['REQUEST_URI'];
    $page       =   $_POST['page'];
    if ( $page == NULL ) { ?> 

<form method="post" action="">

<div>LOCATION : <input type="text" name="location"/></div>

<div>DESCRIPTION : <textarea id="details" cols="80" rows="10 maxlength="600" name="details" rows="20"></textarea></div>

<div>UPLOAD IMAGE : <input type="file" name="loc-image" id="loc-image" tabindex="25" /></div>


<input type="hidden" value="1" name="page" />
<input type="hidden" name="action" value="post_action" />
<input type="submit" name="submit" value="PROCEED"/>
</form>

<?php 
} else if ( $page == 1 ) { ?> 

<?php 
$location=$_POST['location'];
$description=$_POST['details'];
$photo=$_POST['loc-image'];

echo 'Location : ' . $location . '</br>';
echo 'Details  : ' . $description . '</br>'; 
echo 'Image    : ' . $photo . '</br></br>';
?>
<?php
}
?>

问题:在最后一步中,我当前的代码能够显示所有提交的数据以及图像名称,但不能显示图像。此时我想显示图像。基本上,我需要最后一步是一个预览页面。

下面给出的代码处理表单,我想提请您特别注意代码中有助于插入附件的特定部分,因为它可能有助于设计解决方案。

表格处理器:

   if( 'POST' == $_SERVER['REQUEST_METHOD'] &&  $_POST['action'] == "post_action") {

                //some error checking done here

                //set vars
                $location       =  $_POST['location'];
                $description =  $_POST['details'];


                if (empty($error)) {   
                $new_post = array(   //insert form inputs, set var and define array
                'post_title'    =>  $location, 
                'post_content'  =>  $description,
                'post_status'   =>  'draft', 
                'post_type' =>  'post',  
                // assigning tags and categoris is no issue
                );

                $pid = wp_insert_post($new_post);

                                        //attachment helper function    
                                        function insert_attachment($file_handler,$post_id,$setthumb='false') {

                                            if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK){ return __return_false(); 
                                            } 
                                            require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                                            require_once(ABSPATH . "wp-admin" . '/includes/file.php');
                                            require_once(ABSPATH . "wp-admin" . '/includes/media.php');

                                            $attach_id = media_handle_upload( $file_handler, $post_id );

                                            //set post thumbnail
                                            if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
                                            return $attach_id;
                                            }


                                        //INSERT OUR MEDIA ATTACHMENTS
                                        if ($_FILES) {
                                        foreach ($_FILES as $file => $array) {
                                        $newupload = insert_attachment($file,$pid);
                                        // $newupload returns the attachment id of the file that
                                        // was just uploaded. Do whatever you want with that now.
                                        }
                                        } // end of attachment statement
    }
    }

脚注:除了问题之外,代码已经过大量定制,并且在我的 wp-install 上运行良好。因此,请忽略代码中的次要因素,例如。验证、安全等。

【问题讨论】:

    标签: php wordpress forms


    【解决方案1】:

    如果我是对的,您没有将图像保存在临时文件夹中,因此请先将图像保存到临时文件夹中,然后使用 [驱动器名称]:/[文件夹名称] /[img_path] 等图像路径在预览页面上显示图像

    提交表单后,从临时文件夹中删除图像并保存到您的原始文件夹中。

    【讨论】:

    【解决方案2】:

    由于我需要在提交表单后进行预览,因此解决方案比我想象的要容易,尤其是当我已经在代码中分配并声明了附件 ID $newupload 时。所以我完全走wordpress路线并使用下面的代码来调用图像,这就像魅力一样。

    <?php echo wp_get_attachment_image( $newupload,'medium' ); ?>
    

    点击链接阅读更多关于wp_get_attachment_image的信息。希望它可以帮助某人。

    【讨论】:

      猜你喜欢
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 2016-12-04
      • 2021-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多