文件上传不在bootstrap模型中工作

时间:2016-09-22 09:04:45

标签: php twitter-bootstrap

<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'];
    }
}

我尝试使用bootstrap模式上传图片但文件未上传
其他输入字段值发布在操作页面上,如何解决此问题 请帮帮我。

1 个答案:

答案 0 :(得分: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>