Postingan lainnya
Upload gambar dengan ajax codeigniter
malam gan... saya upload gambar dengan ajax di codeigniter namun tersimpan didatabase keterangan nya You did not select a file to upload
sementara file gambar tersimpan ke folder...
mohon petunjuknya gan. terimakasih
SCRIPT MODEL UNTUK SIMPAN DATA
public function save($msg){
$data = array(
"no_lhp" => $this->input->post('no_lhp1'),
"idskpd" => $this->input->post('idskpd'),
"idpemeriksa" => $this->input->post('idpemeriksa'),
"tgl_tlhp" => $this->input->post('tgl_tlhp'),
"ket" => $this->input->post('ket'),
"nominal" => format_num($this->input->post('nominal')),
"status" => $this->input->post('status'),
"idrekomendasi" => $this->input->post('idrekomendasi'),
"filename"=>$msg
);
//var_dump($data);
//die;
$this->db->insert('tlhp',$data); // Untuk mengeksekusi perintah insert data
}
SCRIPT CONTROLLER UPLOAD GAMBAR
public function simpan(){
$config['upload_path'] = './assets/document';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('file')){
//$status = "error";
$msg = $this->upload->display_errors();
}
else{
$dataupload = $this->upload->data();
// $status = "success";
$msg = $dataupload['file_name'];
$this->Lhp_model->save($msg);
}
if($this->Lhp_model->validation("save")){ // Jika validasi sukses atau hasil validasi adalah true
//die;
$this->Lhp_model->save($msg); // Panggil fungsi save() yang ada di Lhp_model.php
//$this->Lhp_model->save();
$html = $this->load->view('admin/viewlhp', array('model'=>$this->Lhp_model->view()), true);
$callback = array(
'status'=>'sukses',
'pesan'=>'Data berhasil disimpan',
'html'=>$html
);
}else{
$callback = array(
'status'=>'gagal',
'pesan'=>validation_errors()
);
}
echo json_encode($callback);
}
SCRIPT LINK SIMPAN DENGAN FILE AJAX YANG DIPANGGIL
$('#btn-simpan').click(function(){ // Ketika tombol simpan di klik
$('#loading-simpan').show() // Munculkan loading simpan
var file_data = $('#gambar').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({
url: '<?php echo site_url("lhp/simpan") ?>', // point to server-side PHP script
dataType: 'json', // what to expect back from the PHP script, if anything
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
beforeSend: function(e) {
if(e && e.overrideMimeType) {
e.overrideMimeType('application/jsoncharset=UTF-8')
}
},
success: function(response){ // Ketika proses pengiriman berhasil
$('#loading-simpan').hide() // Sembunyikan loading simpan
if(response.status == 'sukses'){ // Jika Statusnya = sukses
// Ganti isi dari div view dengan view yang diambil dari proses simpan.php
$('#view').html(response.html)
/*
* Ambil pesan suksesnya dan set ke div pesan-sukses
* Lalu munculkan div pesan-sukes nya
* Setelah 10 detik, sembunyikan kembali pesan suksesnya
*/
$('#pesan-sukses').html(response.pesan).fadeIn().delay(10000).fadeOut()
$('#form-modal').modal('hide') // Close / Tutup Modal Dialog
// Bersihkan teks saat modal di close
document.getElementById("idrekomendasi").value = "";
document.getElementById("no_lhp").value = "";
document.getElementById("no_lhp1").value = "";
document.getElementById("tgl_lhp").value = "";
document.getElementById("idskpd").value = "";
document.getElementById("idpemeriksa").value = "";
document.getElementById("thn_periksa").value = "";
document.getElementById("obyek_periksa").value = "";
document.getElementById("uraian").value = "";
document.getElementById("skpd").value = "";
document.getElementById("pemeriksa").value = "";
}else{ // Jika statusnya = gagal
/*
* Ambil pesan errornya dan set ke div pesan-error
* Lalu munculkan div pesan-error nya
*/
$('#pesan-error').html(response.pesan).show()
}
},
error: function (xhr, ajaxOptions, thrownError) { // Ketika terjadi error
alert(xhr.responseText) // munculkan alert
}
})
})
SCRIPT AJAX DI FORM
<script>
$(document).on('click','#btn-simpan',function(e){
e.preventDefault();
var file_data = $('#gambar').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({
url: '<?php echo site_url("lhp/simpan") ?>', // point to server-side PHP script
dataType: 'json', // what to expect back from the PHP script, if anything
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function(data,status){
//alert(php_script_response); // display response from the PHP script, if any
if (data.status!='error') {
$('#gambar').val('');
alert(data.msg);
}else{
alert(data.msg);
}
}
});
})
</script>
0
Belum ada Jawaban. Jadi yang pertama Jawaban
Login untuk ikut Jawaban