php - how to set the value of same input filed but in different row through ajax

I have a form I can create a new row through jquery. The problem I'm facing is that when I select the product from dropdown it have to set the rate of this product to its next column. The first row does it fine. But when i create the new row and select the product it gives the correct value but sets value on first row's column. I don't know how to set value to corresponding column. Screenshot
As you can see in the picture I select the milk product, it sets the value to next column when I created the new row and select the product it sets value to the first row's column.
code of function which returns the dropdown values of product
function fill_unit_select_box($con)
{
$output = '';
$query = "SELECT * FROM `stock` INNER JOIN product ON stock.product_id = product.product_id
WHERE stock.product_id = product.product_id ORDER BY product_name ASC";
$query1=mysqli_query($con,$query);
while($row=mysqli_fetch_array($query1))
{
$output .= '<option value="'.$row["product_id"].'">'.$row["product_name"].'</option>';
}
return $output;
}
code of form created through jquery
$(document).on('click', '.add', function(){
var html = '';
html += '<tr>';
html += '<td><select name="item_name[]" onchange="product_id(this)" class="form-control item_name"><option value="">Select Unit</option><?php echo fill_unit_select_box($con); ?></select></td>';
html += '<td><input type="text" disabled name="item_per_unit_price[]" id="product_price" class="form-control item_per_unit_price input" /></td>';
html += '<td><input type="text" name="item_quentity[]" class="form-control item_quentity input" /></td>';
/*html += '<td><input type="text" name="item_total_price[]" class="form-control item_total_price" /></td>';*/
html += '<td><button type="button" name="remove" class="btn btn-danger btn-sm remove"><span class="glyphicon glyphicon-minus"></span></button></td></tr>';
$('#item_table').append(html);
});
code of onchange function which sets the value in price per unit
function product_id(str) {
var id = str.value;
$.ajax({
type : "POST",
url : "stock.php?page=stock_per_unit_price",
data : {
id:id
},
success: function (data) {
console.log(data);
$("#product_price").val(data);
}
});
}
$("#product_price").val(data); I don't know how to set value on same column of different row.
Answer
Solution:
To set the price in the corresponding row, it's necessary to pass a second parameter to yourproduct_id()
function to identify the current row. This could be done like in the following example:
$(document).ready(function() {
$(document).on("change", "select", function() {
let value = $(this).val();
let row = $(this).closest("tr").index();
product_id(value, row);
});
function product_id(value, row) {
for (var k in dataset) {
if (value == k) {
let price = dataset[k];
$("tr:eq(" + row + ")").find(".product_price").val(price)
}
}
}
dataset = {"1" : "10 €", "2" : "20 €"}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td><select>
<option value="">Select Unit</option>
<option value="1">1</option>
<option value="2">2</option>
</select></td>
<td><input class="product_price"/></td>
</tr>
<tr>
<td><select>
<option value="">Select Unit</option>
<option value="1">1</option>
<option value="2">2</option>
</select></td>
<td><input class="product_price"/></td>
</tr>
</table>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: using $this when not in object context laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JQuery
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience.
https://jquery.com/
MySQL
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
HTML
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.