I am trying to add multiple products by scanning barcodes in my invoice form, which I have succeeded in creating the form where I can add multiple rows to the form and also can scan barcode and add products automatically to the row. But here the problem is after scanning the barcode for the first row, it displays the exact product details but for the next row, after scanning different barcode, it shows the same result as in the first row and its same for all the rows added.
this is my ajax code
$(document).ready(function()
{
getProd();
addnewrow();
removerow();
})
function addnewrow()
{
$('#add_row').click(function()
{
var n=($('.detail tr').length-0)+1;
var tr = '<tr>'+
'<td class="slno">'+n+'</td>'+
'<td><input type="text" onkeydown="getProd();" name="barcode[]" id="barcode'+n+'" class="form-control input-sm barcode" autofocus><input type="hidden" name="item_name[]" id="item_name1" class="form-control input-sm item_name"></td>'+
'<td><input type="text" name="order_item_qty[]" id="order_item_quantity'+n+'" data-srno="1" class="form-control input-sm order_item_quantity" /></td>'+
'<td><input type="text" name="order_item_price[]" id="order_item_price'+n+'" data-srno="1" class="form-control input-sm number_only order_item_price" /></td>'+
'<td><input type="text" name="order_item_amount[]" id="order_item_actual_amount'+n+'" data-srno="1" class="form-control input-sm order_item_amount" disabled /></td>'+
'<td><input type="text" name="order_item_cgst_rate[]" id="order_item_tax1_rate'+n+'" data-srno="1" class="form-control input-sm number_only order_item_cgst_rate" /></td>'+
'<td><input type="text" name="order_item_cgst_amount[]" id="order_item_tax1_amount'+n+'" data-srno="1" readonly class="form-control input-sm order_item_cgst_amount" /></td>'+
'<td><input type="text" name="order_item_sgst_rate[]" id="order_item_tax2_rate'+n+'" data-srno="1" class="form-control input-sm number_only order_item_sgst_rate" /></td>'+
'<td><input type="text" name="order_item_sgst_amount[]" id="order_item_tax2_amount'+n+'" data-srno="1" readonly class="form-control input-sm order_item_sgst_amount" /></td>'+
'<td><input type="text" name="order_item_disc_amount[]" id="order_item_disc_amount'+n+'" data-srno="1" readonly class="form-control input-sm order_item_disc_amount" /></td>'+
'<td><input type="text" name="order_item_final_amount[]" id="order_item_final_amount'+n+'" data-srno="1" readonly class="form-control input-sm order_item_final_amount" /></td>'+
'<td><a href="#" class="remove"></td>'+
'</tr>';
$('.detail').append(tr);
});
}
function removerow()
{
$('body').delegate('.remove','click',function()
{
$(this).parent().parent().remove();
});
}
function getProd()
{
var n=($('.detail tr').length-0)+1;
var bar = document.getElementById("barcode"+n);
$(bar).keydown(function (e){
if (e.key === "Enter") {
var barcode = $(bar).parent().parent().find('.barcode').val();
console.log(barcode);
$.ajax({
url : "../system/ajax/sales/getdata.php",
method : "POST",
data : {s_barcode:barcode},
dataType : 'JSON',
success : function (data)
{
console.log(data);
$(bar).parent().parent().find('.barcode').attr('type','hidden');
$(bar).parent().parent().find('.item_name').attr('type','text');
$(bar).parent().parent().find('.item_name').val(data.prod_name);
$(bar).parent().parent().find('.order_item_quantity').val(1);
$(bar).parent().parent().find('.order_item_price').val(data.prod_rate);
$(bar).parent().parent().find('.order_item_cgst_rate').val(data.gst_rate/2);
$(bar).parent().parent().find('.order_item_sgst_rate').val(data.gst_rate/2);
$(bar).parent().parent().find('.order_item_disc_amount').val(data.disc_rate);
}
});
}
})
$('.barcode').attr('type','text');
$(bar).parent().parent().find('.item_name').val('');
$(bar).parent().parent().find('.order_item_quantity').val('');
$(bar).parent().parent().find('.order_item_price').val('');
$(bar).parent().parent().find('.order_item_cgst_rate').val('');
$(bar).parent().parent().find('.order_item_sgst_rate').val('');
$(bar).parent().parent().find('.order_item_disc_amount').val('');
}
this is my data functions
<?php
include('../../../config.php');
function GetRecord()
{
global $dbc;
$bar = mysqli_real_escape_string($dbc, $_POST['s_barcode']);
$q = "SELECT * FROM barcode WHERE slno=? LIMIT 1";
$bar_data = getSingleRecord($q, 's', [$bar]);
$prodId = $bar_data['bar_prod_id'];
$q1 = "SELECT p.*,g.gst_rate,d.disc_rate FROM products p
LEFT JOIN gst g ON g.gst_id=p.prod_gst
LEFT JOIN discount d ON d.disc_id=p.prod_disc
WHERE p.prod_id=? LIMIT 1";
$prod = getSingleRecord($q1, 'i', [$prodId]);
echo json_encode($prod);
}
?>