I’m trying to create a purchase order website. The portion of the form where the list of items is listed is giving me some problems. Basically, I’m trying to clone a row without its values. I’ve read several articles online about this issue, but I’m still having a hard time figuring out how to get this to work in my particular scenario. Any suggestions would be much appreciated. Also, any recommended reading would be awesome. Also, I’m struggling with how to display a line# for each line - my global is clearly not working. :-/
<form action="index.php" method="POST">
<fieldset>
<legend>Items:</legend>
<?php global $number;
// $number=1
?>
<div class="table" id="tbl">
<div class="tr">
<div class="td bold">Line#</div>
<div class="td bold">Contract:</div>
<div class="td bold">Description:</div>
<div class="td bold">Qty:</div>
<div class="td bold">Each:</div>
<div class="td bold">Price total:</div>
</div>
<div class="tr">
<div class="td"><?php echo $number; $number++; ?></div>
<!-- <div class="td"><input type="text" name="" /></div>-->
<div class="td"><input class="td contract" type="text" name="contract[]" /></div>
<div class="td"><input class="td descr" type="text" name="itemDescription[]" /></div>
<div class="td"><input type="number" name="itemQuantity[]" keyup()
id="qty" step='0.01' value='0.00' placeholder='0.00' class="td qty" /></div>
<div class="td"><input type="number" name="itemCostEach[]"
id="price" step='0.01' value='0.00' placeholder='0.00' class="td price each" /></div>
<div class="td"><input type="number" name="itemTotal[]"
id="total" step='any' value='0.00' placeholder='0.00' class="td price total" /></div>
<div class="td"><input type="button" class="button"
value="Add item" onclick="addRow(this);" /></div>
</div>
<!-- <div class="tr">-->
<!-- <div class="td"></div>-->
<!-- <div class="td"></div>-->
<!-- <div class="td"></div>-->
<!-- <div class="td"></div>-->
<!-- <div class="td">Grand total:</div>-->
<!-- <div class="td"><input type="number" name="grandTotal[]"-->
<!-- id="grandTotal" step='any' value='0.00' placeholder='0.00' class="td price total" /></div>-->
<!-- </div>-->
</div>
<script>
$('input').keyup(function(){ // run anytime the value changes
var firstValue = Number($('#qty').val()); // get value of field
var secondValue = Number($('#price').val()); // convert it to a float
// $('#total').html(firstValue + secondValue + thirdValue + fourthValue); // add them and output it
document.getElementById('total').value = firstValue * secondValue;
// add them and output it
});
</script>
</fieldset>
</form>
<script type="text/javascript">
function addRow(n)
{
var tr = n.parentNode.parentNode.cloneNode(true);
for(var i = 0; i < tr.length; i++)
{
tr[i].innerHTML = "";
}
document.getElementById('tbl').appendChild(tr);
}
</script>