skip to Main Content

I have input with date type and checkbox for job history. Every working period that is still running will select checkbox input. How to display the checkbox value to the input form and save it to the database?

Form Input

enter image description here

Code

<div class="form-group">
    <label for="recipient-name" class="col-form-label">Tanggal Akhir Kerja</label>
    <input type="date" class="form-control" name="f_tahunakhir" value="<?= set_value('f_tahunakhir');?>" autocomplete="off">
    <input type="checkbox" class="ui-datepicker-current" name="current_date"> Sampai dengan saat ini
</div>

2

Answers


  1. Chosen as BEST ANSWER

    enter image description here

    Controller addData

    if ($this->form_validation->run() == false)
        {
            $this->load->view('templates/header', $data);
            $this->load->view('templates/top_bar');
            $this->load->view('templates/sidebarmenu');
            $this->load->view('pekerjaan/tambah');
            $this->load->view('templates/footer');
        }else
        {
            if ($data)
            {
                $array = [
                    'id_akun'           => $data['akun']['id'],
                    'instansi'          => $this->input->post('f_instansi'),
                    'jabatan'           => $this->input->post('f_riwayat_jab'),
                    'status'            => $this->input->post('f_status'),
                    'mulai_bekerja'     => $this->input->post('f_tahunmulai'),
                    'selesai_bekerja'   => $this->input->post('f_tahunakhir'),
                    'u_tugas'           => $this->input->post('f_u_tugas'),
                    'date_created_kerja' => time()
                ];
    
                //insert to database
                $this->db->insert('pekerjaan', $array);
    
                $this->session->set_flashdata('message', '<div class="text-center alert alert-default-success" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>Data berhasil ditambahkan</div>');
                redirect('pekerjaan/index');
            }else
            {
                $this->session->set_flashdata('message', '<div class="text-center alert alert-default-danger" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>Data gagal ditambahkan</div>');
                redirect('pekerjaan/index');
            }
        }
    

  2. I think you are trying to capture job history information using date inputs and a checkbox to indicate if the job is still ongoing. When the checkbox is selected, you want to set the date input value to the current date you can achieve this using HTML, JavaScript, and PHP.

    <div class="form-group">
        <label for="recipient-name" class="col-form-label">Tanggal Akhir Kerja</label>
        <input type="date" class="form-control" name="f_tahunakhir" id="f_tahunakhir" value="<?= set_value('f_tahunakhir');?>" autocomplete="off">
        <input type="checkbox" class="ui-datepicker-current" id="current_date" name="current_date" onchange="handleCheckboxChange()">
    </div>
    
    <script>
    function handleCheckboxChange() {
        const dateInput = document.getElementById('f_tahunakhir');
        const checkbox = document.getElementById('current_date');
        
        if (checkbox.checked) {
            const currentDate = new Date().toISOString().substr(0, 10); // Format: YYYY-MM-DD
            dateInput.value = currentDate;
            dateInput.disabled = true; // Optionally, disable the date input when checkbox is checked
        } else {
            dateInput.value = ''; // Clear the input value
            dateInput.disabled = false; // Enable the date input
        }
    }
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search