donglu9978 2017-03-02 03:51
浏览 24

Codeigniter :: alert用javascript通知max_size

i make alert if image size > to max_size with javascript. but why always appeared alert "too big picture" when the image size under max_size. there was something I forgot?

my view :

<?php echo form_open_multipart('#', array('id' => 'form-produk'));?>
<input type="text" name="nama" class="form-control" id="nama" placeholder="Nama produk" required>
<input type="file" name="gambar" class="form-control" id="gambar" required>
<input type="file" name="gambar_tambah" class="form-control" id="gambar" required>
<button type="submit" style="background-color:#1c2d3f;" class="simpan_produk btn btn-primary">Simpan Produk</button>
<?php echo form_close();?>

my javascript :

$('.simpan_produk').click(function(){
    var UrlToPass = $("#form-produk").serialize();
    $.ajax({ // Send the credential values to another checker.php using Ajax in POST menthod
        type : "POST",
        data : UrlToPass,   
        url  : baseURL + "trueaccon2194/produk/proses_tambah_produk",
    beforeSend: function(){
        $('.simpan_produk').prop('disabled', true);
        $('.simpan_produk').html('sedang menyimpan...'); //Loading button text 
    },
    success : function(write){
        $('.simpan_produk').prop('disabled', false);
        $('.simpan_produk').html('Simpan'); //reset button text to original text
            if (write=="datasuccesswrite"){
                alert('data is saving!');
                window.location.href = "../";
            }else if(write=="imageistoolarge"){
                alert('too big picture!');
                location.reload();
            }
        }
    });
return false;
});

my controller :

function proses_tambah_produk(){
    $config['upload_path']          = 'assets/img/produk';
    $config['allowed_types']        = 'gif|jpg|png|jpeg';
    $config['max_size']             = 300;
    $config['overwrite']            = TRUE;
    //$config['maintain_ratio']         = TRUE;
    //$config['create_thumb']           = TRUE;
    //$config['max_width']            = 75;
    //$config['max_height']           = 50;
    $this->load->library('upload', $config);

    if($_FILES['gambar']['size'] > 300){
         log_helper("produk", "Gagal Menambah produk baru");
         echo"imageistoolarge";
         //redirect('trueaccon2194/produk/tambah_produk');
    }else if(!$this->upload->do_upload('gambar_tambah')){
         log_helper("produk", "Gagal Menambah produk baru");
         echo"imageistoolarge";
         //redirect('trueaccon2194/produk/tambah_produk');
    }else{

    $result=array();
    $files = $_FILES;
    $count = count($_FILES['gambar_tambah']['name']);
    for($i=0; $i<$count; $i++)
            {
            $_FILES['gambar_tambah']['name']= $files['gambar_tambah']['name'][$i];
            $_FILES['gambar_tambah']['type']= $files['gambar_tambah']['type'][$i];
            $_FILES['gambar_tambah']['tmp_name']= $files['gambar_tambah']['tmp_name'][$i];
            $_FILES['gambar_tambah']['error']= $files['gambar_tambah']['error'][$i];
            $_FILES['gambar_tambah']['size']= $files['gambar_tambah']['size'][$i];
            $this->upload->do_upload('gambar_tambah');
            $upload_data = $this->upload->data();
            $name_array[] = $upload_data['file_name'];
            $fileName = $upload_data['file_name'];
            $images[] = $fileName;
            $result[] = $files['gambar_tambah']['name'][$i];
            }

        $target = $this->input->post('nama');                   
            $this->upload->do_upload('gambar');
            $gambar = $_FILES['gambar']['name'];
            $data = $this->input->post();
            $data['id'] = $this->data['id'];
            $this->produk_adm->add($data, $gambar, $result);
            log_helper("produk", "Menambah Produk ".$target."");
            echo "datasuccesswrite";
            //redirect('trueaccon2194/produk');

    }
}
  • 写回答

2条回答 默认 最新

  • dongliang9682 2017-03-02 03:57
    关注

    In your view id must be unique for each element.So make id of second file id="gambar_tambah".

    <?php echo form_open_multipart('#', array('id' => 'form-produk'));?>
    <input type="text" name="nama" class="form-control" id="nama" placeholder="Nama produk" required>
    <input type="file" name="gambar" class="form-control" id="gambar" required>
    <input type="file" name="gambar_tambah" class="form-control" id="gambar_tambah" required>
    <button type="submit" style="background-color:#1c2d3f;" class="simpan_produk btn btn-primary">Simpan Produk</button>
    <?php echo form_close();?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改