How to fetch rate of a product from db which is selected in select box in array

Posted 4 months ago by AbdulBazith

Guys iam working with a project inventory control

i have a purchase order form which is table structure product name(select box), quantity(textbox) and rate(textbox with readonly)

i have a add button also when admin clicks it a new row with another select box, quantity and rate is raised. it is done using javascript

i have given array for all these three,

this is form blade file


 <thead>
 <th>Product</th>
 <th>Quantity</th>
 <th>rate</th>
 <th>Action</th>
</thead>

<tr>

    <td>
        <select id="product_name" name="product_name[]">
            @foreach($products as $product)
            <option value='{{ $product->id }}'>{{$product->product_name }}</option>
            @endforeach
        </select>
    </td>
    <td>
        <input type="text" name="qty[]" id="qty"  value="10" />
    </td>
    <td>
        <input type="text" name="rate[]" id="rate"  readonly />
    </td>
    <td>
        <input type="button" id="aa"  onclick="insRow()" value="Add">
</tr>

and this is my javascript

function insRow() {

        var table = document.getElementById('purchase_table');
        var rowCount = table.rows.length;

        var row = table.insertRow(rowCount);
        var colCount = table.rows[1].cells.length;
        for (var i = 0; i < colCount; i++) {
            var newcell = row.insertCell(i);
            newcell.innerHTML = table.rows[1].cells[i].innerHTML;
        }
    }

Everything works fine, but what the doubt is all the names are in arrays,

product_name[], qty[], rate[]

if admin select a product name in that select box say for example "Rice" then in the rate select box the rate of that product must be come.

i know this could be done using Ajax only.

whats the problem is if there is one select box and one text box , i will use ajax and if that select box changes based on that the rate will be fit to text box. with id i do this.

But here it is array, and id's are same for all so how can i match with product item to rate from db using ajax.

Another doubt,

after fetching this how can i save it into db ??

that is my purchase table contain

product_name, 
product_qty, 
product_rate

so from the from admin may send 10 product name with 10 product qty with 10 product rate.

so in my table it must be inserted as 10 rows , how can i do this??

Kindly please answer..

iam struggling

Please suggest your answers

Please sign in or create an account to participate in this conversation.