以前添加的图像与新添加的图像一起显示,并插入到表中

previous added images are displaying along with the newly added images and also inserting in to the table

本文关键字:添加 图像 插入 显示 新添加 一起      更新时间:2023-09-26

当i正在插入以前添加的图像时,将显示。任何苏尔

这是我的视图页面

  <div class="col-md-2">
           <form enctype="multipart/form-data" method="post" action="<?php echo base_url();?>admin_control/upload_image" id="upload-image">
            <div class="form-group upload-image">
               <input type="file" data-toggle="tooltip" data-placement="bottom" title="Upload Image" name="file" id="file">
            </div>
           </form>
        </div>
        <div class="clearfix"></div>
        <div class="col-md-12" style="padding-left: 0px;" id="uploaded-img">
       </div>

<script type="text/javascript">
    $(document).ready(function (){
    $('#modal-close').click();
       $("#file").change(function() { $('#upload-image').submit(); });
        $('#upload-image').on('submit',(function(e){
           e.preventDefault();
           //alert()
            var formData = new FormData(this);
            $.ajax({
                type:'POST',
                url: $(this).attr('action'),
                data:formData,
                cache:false,
                contentType: false,
                processData: false,
                success:function(result)
                {
                   //$('#uploaded-img').html(result);
                    //location.reload();
                }
            });
        }));
      });

我想问题出在 JavaScript 上,我在这里苦苦挣扎......

我真的不确定你为什么要这样做

$("#file").change(function() { $('#upload-image').submit(); });

请尝试将代码更改为

$('#file').on('change',(function(e){
       e.preventDefault();
       //alert()
        var formData = new FormData(this);
        $.ajax({
            type:'POST',
            url: $(this).attr('action'),
            data:formData,
            cache:false,
            contentType: false,
            processData: false,
            success:function(result)
            {
               //$('#uploaded-img').html(result);
                //location.reload();
            }
        });
    }));

在这种情况下,当您选择一个文件而不是调用其他元素的提交时,我正在上传一个文件。