Postingan lainnya
Buku Ini Koding!
Baru!
Buku ini akan jadi teman perjalanan kamu belajar sampai dapat kerjaan di dunia programming!
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 [];
}
Belum ada Jawaban. Jadi yang pertama Jawaban
Login untuk ikut Jawaban