2016-09-22 67 views
1
<a href="#" data-toggle="modal" data-target="#contact_dialog"> 
Change Picture 
</a> 

這裏不工作是我的形式
文件上傳自舉模式

<form id="picture_change" class="form-horizontal" action="include/picture_change.php" method="post" enctype="multipart/form-data"> 
       <div class="form-group"> 
       <input type="text" name="vals"> 
       <h5 style="padding-top:5px; padding-bottom:5px;">Change Picture</h5> 
    <input type="file" name="proimg" class="file"> 
    <div class="input-group col-xs-12"> 
     <span class="input-group-addon"><i class="glyphicon glyphicon-picture"></i></span> 
     <input type="text" class="form-control input-sm" name="proimg" disabled placeholder="Upload Image"> 
     <span class="input-group-btn"> 
     <button class="browse btn btn-primary input-sm" type="button"><i class="glyphicon glyphicon-search"></i> Browse</button> 
     </span> 
    </div> 
    </div> 
     </form> 
關閉
<script> 
/* must apply only after HTML has loaded */ 
$(document).ready(function() { 
    $("#picture_change").on("submit", function(e) { 
     var postData = $(this).serializeArray(); 
     var formURL = $(this).attr("action"); 
     $.ajax({ 
      url: formURL, 
      type: "POST", 
      data: postData, 
      success: function(data, textStatus, jqXHR) { 
       $('#contact_dialog .modal-header .modal-title').html("Result"); 
       $('#contact_dialog .modal-body').html(data); 
       $("#submitForm").remove(); 
      }, 
      error: function(jqXHR, status, error) { 
       console.log(status + ": " + error); 
      } 
     }); 
     e.preventDefault(); 
    }); 

    $("#submitForm").on('click', function() { 
     $("#picture_change").submit(); 
    }); 
}); 
</script> 

picture_change.php

 $imgFile = $_FILES['proimg']['name']; 
$tmp_dir = $_FILES['proimg']['tmp_name']; 
$imgSize = $_FILES['proimg']['size']; 
if(!empty($imgFile)) 
{ 
    $upload_dir = '../images/profile_picture/'; // upload directory 
    $images = $ob->imageupload($imgFile,$tmp_dir,$imgSize,$upload_dir); 
    if($images['ermsg'] == '') 
    { 
     $ob->upddata("update tbl_safety_pros_signup set image='".$images['userpic']."' where id='".$_SESSION['user_id']."'"); 
    } 
    else { 
     echo $msgs=$images['ermsg']; 
    } 
} 

我嘗試使用引導模式上傳圖片,但文件未上傳
其他輸入字段值在操作頁面上發佈,如何解決此問題。
請幫幫我。

+0

http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously –

回答

0
<script> 
$('#picture_change').submit (function (event) { 

    event.preventDefault (); 
    event.stopPropagation (); 

    var $scriptUrl = $('#picture_change').attr ('action'); 
    var $postData = new FormData($('#picture_change')[0]); 

    $.ajax ({ 
     method : 'POST', 
     url : $scriptUrl, 
     data : $postData, 
     cache : false, 
     processData: false, 
     contentType: false, 
     dataType : 'json', 
     success : function (data, textStatus, jqXHR) { 
      if (data.success === true) { alert ('success'); } 
      else { alert ('failure'); } 
     }, 
     error : function (jqXHR, textStatus, errorThrown) { 
      alert ( jqXHR.responseText );/*This returns the empty array*/ 
     } 
    }); 

}); 
</script> 
+0

未插入遺憾的文件到DB – smijith

+0

好第一次嘗試沒有Ajax上傳搞清楚的是任何PHP代碼錯誤....這是我用了很長時間...然後使用這個腳本...如果仍然不工作,請讓我知道,我會給你發另一個代碼 –

+0

感謝@Veshraj它現在的工作,對不起,我的小錯。 – smijith