In a web application, used for registration of rooms and used products, I generate a list of rows with several inputfields (see code #1). The code simply adds a row with some input fields. Also a dynamic dropdown-list generated with PHP echoed into the code (see also code #1).
The rows are nicely generated and the droplist are functioning normally. However the code is used to add products to an order. Those products have a default price into the database (which can be edited by a different page...).
Each row generated does look like code #2, however adjusted for readability. When I pick a product from the list the 2 other fields have to get the price inserted. The input field to change the price for this order only and a text part for reference only.
Code #3 shows the part of code which created the call and returns the value in the required locations.
This code does work great of the first row, but when I change 2nd of any other of the dropdownlist nothing does change.
My question is how do I make it work that when I select any product in any dropdown list generated to show the price into the corresponding input fields / div?
I have not a single to clue to solve this problem. Please help.
Edit 1: Code #2 and #3 changed to use 'class' instead of 'id'. Me as a bad person did use id's and not classes while having the same fields several times... Please forgive my stupid mistake. But this does not solve the problem about only the first row being modified even if I add the row several times.
Maybe asking my question on a different wat makes it more easy to understand my problem, while code stays te same.
I have several rows with a PHP generated select-box, an empty input box type number and a empty div for futher reference only.
Example in dummy mode:
<select class product></select><input type="number" name="price[]" class="price"><div class="ajaxprice">
<select class product></select><input type="number" name="price[]" class="price"><div class="ajaxprice">
<select class product></select><input type="number" name="price[]" class="price"><div class="ajaxprice">
<select class product></select><input type="number" name="price[]" class="price"><div class="ajaxprice">
These rows are added with code #1 while the first is already at the page with and 'add' button and not a 'remove' button. When I change the select in the first row both the input and the div are nicely filled by the script in code #3. If one of the other rows are changed nothing does change at all. Nor an alert I inserted for testing purposes. So how do I enter the required information into the div and input when I change n-th selectbox?
code #1: Inserting and removing rows with input fields
var max_rows = 10; // maximum input fields
var wrapper = $("#wrapper_rows");
var counter = 1;
$(".rowBtn").click(function (e) {
e.preventDefault();
if (counter < max_rows) {
counter++;
var row = 'some lay out';
row = row + '<?php echo get_products_add($con, $com); ?>';
row = row + 'some futher layout / closing tags';
$(wrapper).append(row );
}
})
$(wrapper).on("click",".rowminBtn", function(e){
e.preventDefault();
$(this).parent('div').parent('div').remove(); counter--;
})
code #2
<select class="product" name="product[]" >// LOADS OF OPTIONS</select>
<input type="number" class="price" name="price[]">
<div class="ajaxprice"></div>
code #3
$(".product").change(function () {
var productid = $('.product').val();
if (productid != '') {
$.ajax({
url:"get_price.php",
method:"POST",
data: { id:productid },
dataType:"json",
success:function(result){
$(".ajaxprice").html("(Original price: " + result + ")");
$(".price").val(result);
}
})
}
else {
$(".ajaxprice").html('');
$(".price").val("");
}
})