无法通过ajax传递图像的id

时间:2018-04-14 18:08:23

标签: javascript php ajax

我在我的代码中使用Dropzone.js,我通过ajax立即将图像上传到数据库。另外,我有"删除"按钮,当我点击它,它工作正常。这是代码:

<script type="text/javascript">
  Dropzone.autoDiscover = false;
  var base_url = "<?php echo base_url();?>";
  $("div#dropzone").dropzone({ 
    url: base_url + 'cars/addpic',
    addRemoveLinks: true,
    uploadMultiple: true,
    paramName: "files",
    acceptedFiles: "image/*",
    dictDefaultMessage: "<span class='mif-file-upload mif-3x'></span> <br>Faylları seçin görək",
    init : function() {     
            this.on("removedfile", function(file) {
                alert("File " + file.name);
                $.ajax({    
                    type:'post',        
                    url: base_url + 'cars/deletimage/' + '1417.png',   
                    cache:false,      
                    success:function(data){
                      alert("vsyo");
                    }    
                });
            });

        }
  });
</script>

但是,我不知道如何在代码中获取并传递刚刚上传的图片的ID而不是'1417.png'。如何在上传图像后立即将id从表传递到此变量?

Php代码:

public function addpic() {
            $manymanyimages = '';
            $config['upload_path'] = './assets/img/cars/';
            $config['allowed_types'] = 'jpg|png|jpeg|JPG|PNG|JPEG';
            $config['overwrite'] = FALSE;
            $config['remove_spaces'] = TRUE;
            $config['max_size'] = '25000';
            $this->load->library('upload', $config);
            $filesCount = count($_FILES['files']['name']);
            for ($i = 0; $i < $filesCount; $i++) {
                $_FILES['filee']['name'] = $_FILES['files']['name'][$i];
                $_FILES['filee']['type'] = $_FILES['files']['type'][$i];
                $_FILES['filee']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
                $_FILES['filee']['error'] = $_FILES['files']['error'][$i];
                $_FILES['filee']['size'] = $_FILES['files']['size'][$i];
                if (!$this->upload->do_upload('filee')) {
                    // $this->output->set_status_header(500);
                    $this->output->set_output(strip_tags($this->upload->display_errors()));
                } else {
                    $fileData = $this->upload->data();
                    $manyimages = $fileData['file_name'];
                }
            }

            $last_id = $this->db->select('id')->order_by('id',"desc")->limit(1)->get('cars')->row()->id + 1;

            $data = array(
                'img_name' => $manyimages,
                'iki' => $manymanyimages,
                'post_id' => $last_id
            );

            return $this->db->insert('imgs', $data);
        }

0 个答案:

没有答案