weixin_33726318 2016-03-15 09:16 采纳率: 0%
浏览 184

Ajax图片上传到数据库

I'm unable to upload the image by ajax,it always redirect to the page whenever I click submit button and the data is not added to the database as well.

form

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" id="ImageUploadForm" enctype="multipart/form-data">
 <input type="text" name="caption" id="caption">
 <input type="file" name="image" id="ImageBrowse"/>
 <input type="submit" class="btn btn-success" value="Save" />  
</form>

ajax

$(document).ready(function (e) {
    $("#imageUploadForm").on('submit',(function(e) {
        e.preventDefault();
        $.ajax({
            url: "<?php echo base_url();?>adminpromisess/addgal",
            type: "POST",
            data:  new FormData(this),
            contentType: false,
            cache: false,
            processData:false,
            success: function(data) {
                alert(data);
                //$("#gallery-form").load(".gallery-form");
            },
            error: function() {
            }           
        });
    }));
});

add data to database function (controller)

 public function addgal(){
     $caption = $this->input->post('caption');
     $promises = 00;
     $description = $this->input->post('description');
     $image = $_FILES['image']['name'];
     if(move_uploaded_file($_FILES['image']['tmp_name'], 'assets/img/upload/promises_image/'.$image)){

         $data = array(
         'caption' => $caption,
         'promises' => $promises,
         'gal_desc' => $description,
         'image' => $image
         );
         $result = $this->adminpromisesmodel->addGallery($data);
     }else{
         echo "Fail to upload file";
         die();
     }
 }

note:model (query) to save the database is correct so i didn't post it

  • 写回答

2条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 Android URL如何转成视频/音频,可行吗?
      • ¥20 SQL数据查询,子查询
      • ¥15 c++字符串分割问题
      • ¥15 关于#sql#的问题:没有用命令关闭cdc,手动把系统表开启的cdc右键删除了
      • ¥15 vue+uniapp
      • ¥15 android freedom
      • ¥15 使用自定义的类型代替内置类型可行吗
      • ¥15 关于STM32的SPI和ENDAT接口编码器通信的问题
      • ¥15 关于#pdfbox#生成的PDF文件正常,转图片中文乱码的问题,如何解决?
      • ¥15 ADS中有关DAC控件的使用问题