i'm new of codeigniter, i have some problem when using form validation to insert data to database with upload some picture. First when i click submit button without any input , the error info from form validation not showing view. Second when i give some input then i click submit button it just refresh the page. Third i learn to upload some image to database, but i'm not sure it's correct. Please help me, i appreciate every answer and advice.
My view (transfer.php):
<section class="columns is-centered my-5">
<div class="container column is-half">
<form action="<?= base_url() ?>bukti_add" method="POST">
<p class="has-text-warning-light">
<?=
validation_errors();
?>
</p>
<input type="hidden" name="bukti_id" value="">
<input class="input" type="text" name="bukti_nama_pengirim" value="" placeholder="Masukan Nama Pengirim">
<select class="select input" name="bukti_bank_pengirim" id="">
<option value="">---Pilih Bank Pengirim---</option>
<option value="">Mandiri</option>
<option value="">BCA</option>
<option value="">BNI</option>
<option value="">BRI</option>
<option value="">CIMB</option>
<option value="">Bank Jateng</option>
</select>
<input class="input" type="text" name="bukti_jumlah_transfer" value="" placeholder="Masukan Jumlah Transfer">
<div class="file has-name is-danger">
<label class="file-label"">
<input class=" file-input" type="file" name="bukti_img">
<span class="file-cta">
<span class=" file-icon">
<i class="fas fa-upload"></i>
</span>
<span class="file-label">
Choose a fileā¦
</span>
</span>
<span class="file-name" style="background-color: white;">
Pilih File Foto Bukti Transfer
</span>
</label>
</div>
<section>
<button class="button is-danger" name="">
Lanjut
</button>
</section>
</form>
</div>
My model(M_checkout):
private function _uploadImage()
{
$config['upload_path'] = './upload/bukti/';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $this->get_payment_by_id;
$config['overwrite'] = true;
$config['max_size'] = 1024; // 1MB
// $config['max_width'] = 1024;
// $config['max_height'] = 768;
$this->load->library('upload', $config);
if ($this->upload->do_upload('bukti_img')) {
return $this->upload->data("file_name");
}
return "default.jpg";
}
public function bukti_to_db()
{
$data = array();
$data['bukti_nama_pengirim'] = $this->input->post('bukti_nama_pengirim');
$data['bukti_bank_pengirim'] = $this->input->post('bukti_bank_pengirim');
$data['bukti_jumlah_transfer'] = $this->input->post('bukti_jumlah_transfer');
$data['bukti_img'] = $this->_uploadImage();
$this->db->insert('bukti', $data);
$bukti_id_save = $this->db->insert_id();
$sdata = array();
$sdata['bukti_id'] = $bukti_id_save;
$this->session->set_userdata($sdata);
}
My controller :
public function bukti_add()
{
$this->form_validation->set_rules('bukti_nama_pengirim', 'Nama', 'trim|required');
$this->form_validation->set_rules('bukti_bank_pengirim', 'Bank', 'trim|required');
$this->form_validation->set_rules('bukti_jumlah_transfer', 'Jumlah Transfer', 'trim|required');
$this->form_validation->set_rules('bukti_img', 'Bukti Transfer', 'trim|required');
if ($this->form_validation->run()) {
$this->M_checkout->bukti_to_db();
redirect('thank', 'refresh');
} else {
redirect('transfer', 'refresh');
}
}
My database : database
You should print errors while on development. It usualy tells you what's wrong. Ex:
Though i'm pretty sure you can't validate
<input class=" file-input" type="file" name="bukti_img">withform_validationlike that. Since you are validating$_POSTwhile your file is inside the$_FILESvariable. Do the file validation on the file upload function.