显示响应状态 - 使用JQuery上传图像

时间:2017-06-09 05:13:02

标签: php jquery wordpress

我要做的是从网址下载一些图片然后将其上传到WordPress帖子,我的表单如下:

<form method="post" id="upload">
<?php foreach($data as $row): ?>

<label>File Name</label>
<input type="text" id="filename_<?php echo $row->id;?>" name="data[<?php echo $row->id;?>][filename]" value="<?php echo $file_name;?>" />

<label>Title</label>
<input type="text" id="title_<?php echo $row->id;?>" name="data[<?php echo $row->id;?>][title]" value="<?php echo $title;?>" />

<?php endforeach;?> 
<button name="act" value="savepost">Save</button>
</form>
<!-- showing result here -->
<div id="result"></div>

那么,当点击“保存”按钮时...操作将调用 function prc_upload()

function prc_upload() {
   $posts = $_POST['data']; 
   foreach ($posts as $post) {
   $filename  = $post['filename'];
   $title     = $post['title'];
   // Process uplaoding here
   // Process uplaoding here
   // Process uplaoding here
   if (!$fileSaved) {
      $result = $filename.' Can not saved !!';
   } else {
      $result = filename' Saved Succesfuly!!';
   }
}

如何在单击“保存”按钮时使用jquery显示响应结果。

2 个答案:

答案 0 :(得分:0)

将你的img追加到html

$($post).each(function(){
$('#placeIDwhereYouWantToShowImg').append('<img src='+this.imgsrc+' class="if any"/>')
})

答案 1 :(得分:0)

确保您的表单具有enctype =“multipart / form-data”属性。

$new_post = wp_insert_post($post_array);
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){

                update_post_meta($new_post,'_thumbnail_id',$attach_id);
            }