Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 表单提交后的表单数据和图像预览_Php_Wordpress_Forms - Fatal编程技术网

Php 表单提交后的表单数据和图像预览

Php 表单提交后的表单数据和图像预览,php,wordpress,forms,Php,Wordpress,Forms,我使用wordpress cms,我需要一个定制表单的帮助。它允许用户从前端发布。有两个字段和一个图像上载字段。这是一个由两部分组成的表单,第一部分是他提交数据的地方,第二步是他获得他刚刚提交的数据的预览。请记住,这发生在提交之后 表格编号: 脚注:除了这个问题,代码已经过大量定制,在我的wp安装中运行良好。因此,请忽略代码中的次要因素,例如验证、安全性等。如果我没记错,您没有将图像保存在临时文件夹中,请先保存到临时文件夹中,并使用图像路径(如[Drive name]:/[folder name

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

表格编号:


脚注:除了这个问题,代码已经过大量定制,在我的wp安装中运行良好。因此,请忽略代码中的次要因素,例如验证、安全性等。

如果我没记错,您没有将图像保存在临时文件夹中,请先保存到临时文件夹中,并使用图像路径(如[Drive name]:/[folder name]/[img_path]在预览页上显示图像


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

因为表单提交后我需要预览,所以解决方案比我想象的要简单,尤其是当我已经在代码中分配并声明了附件id$newupload时。因此,我完全按照wordpress的路线,使用下面的代码来调用图像,它的工作原理类似于charm

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

按照链接阅读更多关于。希望它能帮助别人。

我想这个链接可能会对你有所帮助。。。谢谢你的帮忙,我现在已经整理好了。走wordpress路线。
   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
    }
    }
<?php echo wp_get_attachment_image( $newupload,'medium' ); ?>