I'm strugglin to make a PHP Form with Dynamic Fields with Auto Fill other fields in JQuery, so far I can add new fields and the autofill works just fine but only for the first field.
The autofill drop down only appears on the first input. How can I make all dynamic form input work with the autofill? For example I have 2 fields. Items_name and Total_stock in dynamic form. I want to if I select Items_name. Autofill for field total_stock.
Here is my ajax code :
<script language="javascript">
function AddMasterDetail() {
var idf = document.getElementById("idf").value;
stre="<div class='form-group' id='srow" + idf + "'><div class='controls'>";
stre=stre+" <div class='col-xs-2'>";
stre=stre+"<select placeholder='Items Name' class='form-control input-sm' name='items_id[]' id='items_id' onchange='return autofill();'>"
+"<option value='' disabled selected>Please Select</option>"
+"<?php foreach($v_items_stock as $row){ echo "<option value='$row->items_id'>$row->items_name</option>"; } ?></select>";
stre=stre+"</div>";
stre=stre+"<div class='col-xs-2'>";
stre=stre+"<input class='form-control input-sm' id='total_stock' placeholder='Total Stock' name='total_stock[]' />";
stre=stre+"</div>";
stre=stre+"<div class='col-xs-1'> <button type='button' class='btn btn-danger btn-sm' title='Hapus Rincian !' onclick='removeFormField(\"#srow" + idf + "\"); return false;'><i class='glyphicon glyphicon-remove'></i></button></div>";
$("#divItems").append(stre);
idf = (idf-1) + 2;
document.getElementById("idf").value = idf;
}
function removeFormField(idf) {
$(idf).remove();
}
function autofill(){
var items_id = document.getElementById('items_id').value;
$.ajax({
url:"<?php echo base_url();?>transaction_sending/cari",
data:'&items_id='+items_id,
success:function(data){
var hasil = JSON.parse(data);
$.each(hasil, function(key,val){
document.getElementById('items_id').value=val.items_id;
document.getElementById('total_stock').value=val.total_stock;
});
}
});
}
</script>