2017-06-29 12 views
0

ajaxを使用して画像を保存するのに問題があります。私はprocessData: falsecontentType: falseのようないくつかのパラメータを追加しても、私はちょうどコンソールで0を得ています。私は問題がajaxからphpにデータを渡していると思います。ワードプレスでAjaxを使用して画像を保存する

AJAX

私は最初の店舗イメージにしようとしている...

jQuery(document).ready(function($) { 

$(".report-mem-btn a#submit-repot-mem-btn").click(function(event){ 
    event.preventDefault(); 
    var reason_for_report   = $('select#reason-for-report').val(); 
    var additional_info    = $('textarea#additional-info').val(); 
    var get_rtmedia_file_details = $('.report-member-form').find('input#file-evidence'); 
    var current_user_id    = $('input#current_user_id').val(); 
    var displayed_user_id   = $('input#displayed_user_id').val(); 
    console.log(get_rtmedia_file_details[0].files[0]); 

    var formData = new FormData(); 
    var individual_file = get_rtmedia_file_details[0].files[0]; 
    formData.append('action', 'report_member_action'); 
    formData.append("file", individual_file); 

    jQuery.ajax({ 
     type : "post", 
     url : report_mail.ajax_url, 
     dataType: 'json', 
     processData: false, 
     contentType: false, 

     data: formData, 

     success: function(response) {    
      console.log('test: '+response); 
     } 
     }) 
}); 

}); 

PHP

add_action('wp_ajax_nopriv_report_member_action', 'report_member_action'); 
add_action('wp_ajax_report_member_action', 'report_member_action'); 

function report_member_action(){ 

$uploadedfile    = $_FILES['file']; 


foreach($uploadedfile as $file) { 
    if(is_array($file)) { 
     $attachment_id = upload_user_file($file); 
    } 
} 


//var_dump($_FILES); 

//echo $uploadedfile; 

echo 'Successfully Reported a Member, Thank you!'; 

exit(); 

} 



function upload_user_file($file = array()) { 
require_once(ABSPATH . 'wp-admin/includes/admin.php'); 
    $file_return = wp_handle_upload($file, array('test_form' => false)); 
    if(isset($file_return['error']) || isset($file_return['upload_error_handler'])) { 
     return false; 
    } else { 
     $filename = $file_return['file']; 
     $attachment = array(
      'post_mime_type' => $file_return['type'], 
      'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)), 
      'post_content' => '', 
      'post_status' => 'inherit', 
      'guid' => $file_return['url'] 
    ); 
     $attachment_id = wp_insert_attachment($attachment, $file_return['url']); 
     require_once(ABSPATH . 'wp-admin/includes/image.php'); 
     $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename); 
     wp_update_attachment_metadata($attachment_id, $attachment_data); 
     if(0 < intval($attachment_id)) { 
     return $attachment_id; 
     } 
    } 
    return false; 
} 

答えて

0

のAjaxのjQueryを:これは私が試したものですこれはです:

jQuery(".updateprofile").on('click',function() { 
      alert('helloo updateprofile'); 
      jQuery('#updatprofloader').css('display','block'); 
      var formData1 = new FormData(jQuery('#updatprofl')[0]); 
      jQuery.ajax({ 
          type : "POST", 
          url : ajaxurl, 
          data : formData1, 
          processData:false, 
          contentType: false 
       }).done(function(response) { 

          /* var data = jQuery.parseJSON(response); */ 
          alert(response); 
       }); 

フォームのコードは次のようである:

<form action="javascript:void(0)" id="updatprofl" enctype="multipart/form-data"> 

      <div class="form-group"> 
      <label for="displaynm">Display Name : </label> 
      <input type="text" name="displaynm" class="form-control" id="displaynm"> 
      </div> 
      <div class="form-group"> 
      <label for="aboutme">About Me : </label> 
      <textarea class="form-control" name="aboutme" id="aboutme" placeholer="About Me"></textarea> 
      </div> 

      <div class="form-group"> 
      <label for="aboutme">Profile pic : </label> 
      <div class="input-group"> 
       <label class="input-group-btn"> 
        <span class="btn btn-primary"> 
         Browse&hellip; <input type="file" name="profpicupload" style="display: none;" multiple> 
        </span> 
       </label> 
       <input type="text" class="form-control" readonly> 
      </div> 
      </div> 
      <input type="hidden" name="action" value="ajax_updateprofile" /> 
      <input type="submit" name="updateprofile" class="btn btn-default updateprofile" value="Update Profile"/> 
     </form> 

のfunctions.php

add_action('wp_ajax_ajax_updateprofile', 'ajax_updateprofile'); 
add_action('wp_ajax_nopriv_ajax_updateprofile', 'ajax_updateprofile'); 
function ajax_updateprofile(){ 
    global $wpdb; 
     $disnm = $_POST['displaynm']; 

     echo $disnm; 

    exit; 
} 

では、あなたがこのURLに私の答えを見ることができる画像アップロードコードにしたい場合:Front End Upload Images Wordpress

関連する問題