使用codeigniter中的另一个表单文本输入上传图像?

时间:2016-04-07 04:33:16

标签: php jquery html ajax codeigniter

我在使用注册页面等多个输入文本上传图片时遇到了令人困惑的问题。我找到了解决方案,但图像未保存到数据库或文件夹。 请帮助我...

我的admin.php视图



<script>
  //upload img
$('#form').ajaxForm({
       //uploadimg is my form id            
        dataType: 'json',
        success: processJson
});
function processJson(data) {
        if(data.msg=="success"){
             alert('Upload sukses.');
        }
        else{
             alert('Upload gagal.');
        }
}

function add_edulibs()
{
    save_method = 'add';
    $('#form')[0].reset(); // reset form on modals
    $('.form-group').removeClass('has-error'); // clear error class
    $('.help-block').empty(); // clear error string
    $('#modal_form').modal('show'); // show bootstrap modal
    $('.modal-title').text('Tambah Data'); // Set Title to Bootstrap modal title
}

function edit_edulibs(id)
{
    save_method = 'update';
    $('#form')[0].reset(); // reset form on modals
    $('.form-group').removeClass('has-error'); // clear error class
    $('.help-block').empty(); // clear error string

    //Ajax Load data from ajax
    $.ajax({
        url : "<?php echo site_url('edulibs/ajax_edit/')?>/" + id,
        type: "GET",
        dataType: "JSON",
        success: function(data)
        {

            $('[name="id"]').val(data.id);
            $('[name="nama"]').val(data.nama);
            $('[name="nim"]').val(data.nim);
            $('[name="pembimbing"]').val(data.pembimbing);
            $('[name="subyek"]').val(data.subyek);
            $('[name="judul"]').val(data.judul);
            $('[name="tanggal"]').datepicker('update',data.tanggal);
            $('[name="alamat"]').val(data.alamat);
            $('[name="kontak"]').val(data.kontak);
            $('[name="email"]').val(data.email);
            $('[name="penerbit"]').val(data.penerbit);
            $('[name="file_upload"]').val(data.gambar);
            $('#modal_form').modal('show'); // show bootstrap modal when complete loaded
            $('.modal-title').text('Edit Data'); // Set title to Bootstrap modal title

        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert('Error mendapatkan data');
        }
    });
}

</script>

<div class="modal fade" id="modal_form" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h3 class="modal-title">Kelola Data</h3>
            </div>
            <div class="modal-body form">
            <form action="<?php echo base_url('edulibs/do_upload/')?>" method="post" enctype="multipart/form-data" id="form" class="form-horizontal">
                    <input type="hidden" value="" name="id"/> 
                    <div class="form-body">
                        <div class="form-group">
                            <label class="control-label col-md-3">Nama</label>
                            <div class="col-md-9">
                                <input name="nama" placeholder="Nama Lengkap" class="form-control" type="text">
                                <span class="help-block"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3">NIM</label>
                            <div class="col-md-9">
                                <input name="nim" placeholder="Nomor Induk Mahasiswa" class="form-control" type="text">
                                <span class="help-block"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3">Pembimbing</label>
                            <div class="col-md-9">
                                <input name="pembimbing" placeholder="Nama Pembimbing" class="form-control" type="text">
                                <span class="help-block"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3">Subyek</label>
                            <div class="col-md-9">
                                <input name="subyek" placeholder="Subyek" class="form-control" type="text">
                                <span class="help-block"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3">Judul</label>
                            <div class="col-md-9">
                                <textarea name="judul" placeholder="Judul Karya Tulis" class="form-control"></textarea>
                                <span class="help-block"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3">Tanggal Terbit</label>
                            <div class="col-md-9">
                                <input name="tanggal" placeholder="Tahun-Bulan-Hari" class="form-control datepicker" type="text">
                                <span class="help-block"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3">Alamat</label>
                            <div class="col-md-9">
                                <textarea name="alamat" placeholder="Alamat Lengkap" class="form-control"></textarea>
                                <span class="help-block"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3">Kontak</label>
                            <div class="col-md-9">
                                <input name="kontak" placeholder="Nomor Kontak" class="form-control" type="text">
                                <span class="help-block"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3">Email</label>
                            <div class="col-md-9">
                                <input name="email" placeholder="Alamat e-mail" class="form-control" type="text">
                                <span class="help-block"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3">Penerbit</label>
                            <div class="col-md-9">
                                <input name="penerbit" placeholder="Nama Penerbit" class="form-control" type="text">
                                <span class="help-block"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3">Foto</label>
                            <div class="col-md-9">
                                <input type="file" name="file_upload" />
                                <span class="help-block"></span>
                            </div>
                        </div>
                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" id="btnSave" onclick="save()" class="btn btn-primary">Simpan</button>
                <button type="button" class="btn btn-danger" data-dismiss="modal">Batalkan</button>
            </div>
&#13;
&#13;
&#13;

我的edulibs.php控制器

&#13;
&#13;
public function admin()
	{
	    $this->load->view('admin');
	}
function do_upload(){
        $config['upload_path'] = "./uploads/foto/";  
		 $config['allowed_types'] = 'gif|jpg|png|JPEG';
		 $config['file_name'] = url_title($this->input->post('file_upload'));
		 
		$this->upload->initialize($config); 
		 if(!$this->upload->do_upload('file_upload'))
		 {
		 echo $this->upload->display_errors();
		 }
		 else{
		 $data = array(
		 'gambar'=>$this->upload->file_name
		 );
		 $this->edulib_model->insert($data,'edulib');
		 }
    }
&#13;
&#13;
&#13;

我的edulib_model.php

&#13;
&#13;
public function save($data)
	{
		$this->db->insert($this->table, $data);
		return $this->db->insert_id();
	}

public function insert($data,$table){
		$this->db->insert($data,$table);
	}
&#13;
&#13;
&#13;

0 个答案:

没有答案