Tahun baru, skill baru! 🚀. Masukkan kupon "skill2025" untuk diskon 30% di kelas apa saja

Memanggil Field Input Data menggunakan PHP CodeIgniter

Halo, Saya punya halaman penambahan data customer, dihalaman penambahan data ini ada 2 field input dropdown yaitu field "Customer" dan "Invoice No". Saya ingin pada saat saya memilih data "Customer" pada field "Customer", pada field "Invoice No" menampilkan data2 "reference_no" yang berhubungan dengan data Customer yang dipilih.

Customer dan Invice No memiliki 2 table database terpisah.

Sebagai Contoh saya punya data customer bernama PT. ABC, ketika saya memilih PT. ABC pada field "Customer" maka pada field "Invoice No." muncul data2 "reference_no" yang pernah transaksi.

Masalah yang saya hadapi adalah, ketika saya sudah menginput data pada field "Customer", field "Invoice No." tidak menampilkan data apapun.

dibawah ini kodingan view

<script>
    $(document).ready(function () {
    $('#customer').change(function () {
        const customer = $(this).val();
        console.log("Customer Selected:", customer); 

        if (customer) {
            $.ajax({
                url: '<?= site_url('complaint/getInvoicesByCustomer') ?>',
                type: 'POST',
                dataType: 'json',
                data: { customer: customer }, 
                success: function (data) {
                    console.log("Data Received:", data);
                    $('#reference_no').empty().append('<option value="">Select Reference No</option>');
                    if (data.length > 0) {
                        $.each(data, function (key, value) {
                            $('#reference_no').append('<option value="' + value.reference_no + '">' + value.pi_no + ' - ' + value.container1 + '</option>');
                        });
                    } else {
                        $('#reference_no').append('<option value="">No Invoices Found</option>');
                    }
                },
                error: function (xhr, status, error) {
                    console.log("AJAX Error:", xhr.responseText);
                    alert('Error fetching invoice data. Please try again.');
                }
            });
        } else {
            $('#reference_no').empty().append('<option value="">Select Reference No</option>');
        }
    });
</script>


<div class="col-sm-3">
                            <div class="form-group">
                                <?= lang('customer', 'customer'); ?>
                                <?php
                            $customers[''] = lang('select') . ' ' . lang('customer');
                        foreach ($companies as $company) {
                            $customers[$company->id] = $company->company; 
                        }
                        echo form_dropdown('customer', $customers, '', 'id="customer" class="form-control input-tip select" required style="width:100%;"');
                        ?>
                            </div>
                    </div>  


<div class="col-sm-3">
    <div class="form-group">
        <?= lang('invoice_no', 'invoice_no'); ?>
        <?php
            $pi[''] = lang('select') . ' ' . lang('invoice');
            echo form_dropdown('reference_no', $pi, '', 'id="reference_no" class="form-control input-tip select" data-placeholder="' . lang('select') . ' ' . lang('reference') . '" required="required" style="width:100%;" ');
        ?>
    </div>
</div>

dibawah ini kodingan controllernya

public function getInvoicesByCustomer()
{
    $customer = $this->input->post('customer');

    if (!$customer) {
        echo json_encode(['error' => 'Customer ID is required']);
        return;
    }

    $invoices = $this->complaint_model->getInvoicesByCustomer($customer);
    echo json_encode($invoices);
}

dibawah ini kodingan modelnya

public function getInvoicesByCustomer($customer)
{
    $this->db->select('pi_no, reference_no, customer, container1');
    $this->db->where('customer', $customer);
    $q = $this->db->get('invs');

    if ($q->num_rows() > 0) {
        return $q->result_array(); 
    }
    return [];
}
avatar leoniisabell23
@leoniisabell23

3 Kontribusi 0 Poin

Dipost 9 jam yang lalu

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban