-1

I got a dynamic size table. Each row contais ComboBox (product selection), number input (quantity) and disabled number input (total cost). Here is the code that allows to add rows dynamically.

function addRow() {
        var html = "<tr>"; 
        html += "<td><select class='form-control' id='product_input[]' name ='product_input[]' required><option selected disabled>Choose product</option>'";
        html += "<?php $query = "SELECT * FROM products";
                    $result = $conn->query($query);
                    while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
                        echo "<option value='" . $row['ProductID'] . "'>" . $row['ProductName'] . "</option>";
                    } ?>'</select></td>";
        html += "<td><input type='number' class ='form-control' name='quantity_input[]' id='quantity_input[]' required placeholder='Quantity' min='1'/></td>";
        html += "<td><input type='number' class='form-control' name='cost_input[]' id='cost_input[]' value='0' disabled/></td>";
        html += "<td><button type='button' class='btn btn-danger delete btn-block btn-lg' onclick='deleteRow(this);'>Delete</button></td>";
        html += "</tr>";

        var row = document.getElementById("table").insertRow();
        row.innerHTML = html;
    }

I need to display total cost (cost_input) in each row after every product_input selection change. I have tried to make a new function, but idk how to write it properly (site won't load). I can't run this code as before by innerHTML function.

function updateCost(){
        var html = "<?php 
            for($i = 0; $i < count($_POST['product_input']); $i++){
                $productID = $_POST['product_input'][$i];
                $productQuantity = $_POST['quantity_input'][$i];
                $queryGetTotalCost = "SELECT products.UnitPrice * $productQuantity FROM products WHERE products.ProductID= {$productID}";
                $result = $conn->query($queryGetTotalCost);
            }?>";
    }

Could somebody help me please? Been working on it for a while, still can't find the solution.

0 Answers0