Click here to Skip to main content
15,610,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

What I have tried:

this is my form
HTML
<form>
<table class="table table-bordered">
  <thead>
    <th width="4%">SlNo.</th>
    <th width="20%">Item Name</th>
    <th width="7%">Qty</th>
    <th width="8%">Price</th>
    <th width="10%">Actual Amt.</th>
    <th width="15%" colspan="2">CGST (%)</th>
    <th width="15%" colspan="2">SGST (%)</th>
    <th width="5%">Discount</th>
    <th width="13%" rowspan="2">Total</th>
    <th width="3%" rowspan="2">
      <input type="button" value="+" id="add_row" class="btn btn-sm btn-flat btn-primary">
    </th>
  </thead>
  <th>
  <th></th>
  <th></th>
  <th></th>
  <th></th>
  <th width="6%">Rate</th>
  <th>Amt.</th>
  <th width="6%">Rate</th>
  <th>Amt.</th>
  <th></th>
  <th></th>
  </th>
  <tbody class="detail">
    <tr>
      <td class="slno">1</td>
      <td>
        <input type="text" onkeydown="getProd();" name="barcode[]" id="barcode1" 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_qty1" data-srno="1" class="form-control input-sm order_item_quantity" />
      </td>
      <td>
        <input type="text" name="order_item_price[]" id="order_item_price1" 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_amount1" data-srno="1" class="form-control input-sm order_item_amount" readonly />
      </td>
      <td>
        <input type="text" name="order_item_cgst_rate[]" id="order_item_cgst_rate1" 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_cgst_amount1" 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_sgst_rate1" 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_sgst_amount1" 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_amount1" 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_amount1" data-srno="1" readonly class="form-control input-sm order_item_final_amount" />
      </td>
      <td>
        <a href="#" class="remove">
          
      </td>
    </tr>
  </tbody>
  <tfoot>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
    <th colspan="2">Total Payable</th>
    <th style="text-align:center;" class="total">
      ^__b>0
    </th>
  </tfoot>
</table>
<button type="button" value="save" id="btn_bar_add" class="btn btn-sm btn-flat bg-olive pull-right change-btn"> Save</button>
</div>
</form>


this is my ajax code
JavaScript
$(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;
    // const tr = $(this).parent().parent();
    var bar = document.getElementById("barcode"+n);
    // var bar = tr.find('.barcode');
    $(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);
                    // cal_final_total(count);
                }
            });

        }
    })
    $('.barcode').attr('type','text');
    // $('#item_name1').attr('type','hidden');
    $(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
<?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);
}

?>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900