I am in the process of creating a dynamic profit margin calculator for a small project I'm working on.
The brief
- A set of data is pulled from a database table using SQL and PHP
- after some sanity checks, the data is iterated out dynamically as rows in a standard HTML table
- In the process of iterating, we output some of the cells (specifically "cost of production" and "sale price") as inputs so the user can dynamically change the numbers around and see different margins
- The last cell of every row is a margin percentage that is calculated based on the values in the first two input fields
for pseudo code of the following process see snippet 1
The problem
The problem I am running in to is I am trying to use JS to dynamically update the margin field using an onChange() method for both of the inputs. The idea being that if the user updates one of the two fields, the values are read and the margin field updates.
attempts at a solution
- I have tried to output the ids in an array syntax to see if the values would store in an array like
costOfProduction[]. This works in PHP syntax but not in JS from what I've seen - I have tried writing a javascript function like
updateTable(productionCost, salesCost, marginID)which would be calledonChange()and passed the values. The only trouble is I wasn't able to find a way to pull values from adjacent cells into theonChange()call of the original cell. - Scouring the SO forums for a similar fix. All the solutions I have seen have worked with a static single input and a submit button, but I need something more dynamic that does changes on individual inputs which are dynamically output.
code snippits
Snippit 1: output of table elements
<table class="table table-hover table-responsive" id="marginTable">
<thead>
<tr>
<th>product Name</th>
<th>Sale Price</th>
<th>production Cost</th>
<th>Margin</th>
</tr>
</thead>
<tbody>
<?php
try {
/* connect to the database and get the data */
while (hasData){
echo
'<tr>
<td>'.$dishName.'</td>
<td>
<div class="input-group">
<span class="input-group-addon">$</span>
<input class="form-control" type="number" step="0.01" name="itemPrices[]" id="itemPrices" value="'($itemPrice).'" onchange="updateTable()">
</div>
</td>
<td>
<div class="input-group">
<span class="input-group-addon">$</span>
<input class="form-control" type="number" step="0.01" name="productionCost[]" id="productionCost" value="0.00" onchange="updateTable()">
</div>
</td>
<td id="margins"><strong>0.00%</strong></td>
</tr>';
}
}
}
}
echo '<script type="text/javascript">
function updateTable(){
/* javascript goes here */
}
</script>';
?>
</tbody>
</table>
TL;DR
I need a way using JS that given if one of two inputs in a table is updated, a cell on that same row is calculated and updated based on what was input.
Thank you all so much for your time and help!
updateTable()? SO isn't a code-writing service, show us what you tried! :-) Also, to create a minimal, complete, and verifiable example - the problem is in your JS, not your PHP, so we don't need to see that. Show us a row or 2 of generated, static HTML, so we can try to help.