spires Posted April 22, 2009 Share Posted April 22, 2009 Hi Guys. Can some one help with this problem. I have created a function that gets the data from various text fields and places this information into a variable. What I need to happen now, is for each variable to be placed into an specific array. Any ideas? Heres my code. <script language="javascript"> function getTotal() { var selectedQuantity, selectedProduct, selectedItem, selectedUnitPrice, selectedQtyPrice, finalGP; selectedQuantity = document.form1.quantity.value; selectedProduct = document.form1.product.value; selectedItem = document.form1.itemComp.value; selectedUnitPrice = document.form1.unitPrice.value; selectedQtyPrice = document.form1.qtyPrice.value; if (selectedUnitPrice=="0" || selectedUnitPrice=="") { finalGP = (selectedItem - selectedProduct) * selectedQuantity; }else{ finalGP = selectedItem - selectedProduct + selectedUnitPrice * selectedQuantity; } finalUnit = selectedQuantity * selectedUnitPrice; document.form1.qtyPrice.value = finalUnit; document.form1.gp.value = finalGP; f1Array = new Array(10); f1Array[0] = selectedQuantity; } </script> Inside the HTML <script language="javascript"> document.write(f1Array[0]); </script> Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 22, 2009 Share Posted April 22, 2009 Not sure where your problem is. It looks like you are already creating the array and putting at least one value into it at the end of the first block of code. Just put the other values into the array as well. However, your second block of code is probably your problem. That code only runs before the page fully loads. Since, "f1Array" is declared in a function which would not have been run before the page fully loads, you won't get any output from that code. It would be helpful if you provided some more details about what you are trying to achieve. Quote Link to comment Share on other sites More sharing options...
spires Posted April 22, 2009 Author Share Posted April 22, 2009 Hi mjdamato Thanks for your help. What I want to do is build a simple form that calculates our business commissions. See form: http://www.businessmobiles.com/comcalc/main_interface.php Once the add has been clicked, I need the information from each of the fields displayed out side of the form (row 1), then if the form is submitted a second row is add below the last row. I figure that the one way to do this is store the information in an array, then each time the form is submitted, the new row gets added to the array. So, I need to work out how to place each field into it's own array, then keep adding values to the array each time the form is submitted. Any ideas? Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 22, 2009 Share Posted April 22, 2009 Why have the user submit the form for every record? Just let the user add as many records as they want and then submit the form. I already ahve some code that does pretty mch what you want. Let me modify it closer to what you need. I'll post back in a bit. Quote Link to comment Share on other sites More sharing options...
spires Posted April 22, 2009 Author Share Posted April 22, 2009 That would be great, thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 22, 2009 Share Posted April 22, 2009 Well, I was able to rework this, but it seems to only work in IE, not FF - haven't tested other browsers. See if this is what you want. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Untitled Document</title> <script type="text/javascript"> function addComission() { //Reference to input form object var ifo = document.getElementById('input_form'); //Initialize variables var finalGP; var selectedQuantity = ifo.quantity.value; var selectedProduct = ifo.product.value; var selectedItem = ifo.itemComp.value; var selectedUnitPrice = ifo.unitPrice.value; var selectedQtyPrice = ifo.qtyPrice.value; //*********************** // NOTE: This function should have more validation & error handling //*********************** if (selectedUnitPrice=="0" || selectedUnitPrice=="") { finalGP = (selectedItem - selectedProduct) * selectedQuantity; } else { finalGP = (selectedItem - selectedProduct + selectedUnitPrice) * selectedQuantity; } finalUnit = selectedQuantity * selectedUnitPrice; ifo.qtyPrice.value = finalUnit; //ifo.gp.value = finalGP; // Get a reference to the table var tableRef = document.getElementById('commissionTable'); // Insert a row in the table at row index 0 var newRow = tableRef.insertRow(tableRef.rows.length); // Insert the cells right to left addValueField(newRow, 'gp', finalGP) addValueField(newRow, 'qprice', finalUnit) addValueField(newRow, 'uprice', selectedUnitPrice) addValueField(newRow, 'item', selectedItem) addValueField(newRow, 'prod', selectedProduct) addValueField(newRow, 'qty', selectedQuantity); //Remove values from the input form ifo.quantity.value = ''; ifo.product.value = ''; ifo.itemComp.value = ''; ifo.unitPrice.value = ''; ifo.qtyPrice.value = ''; } function addValueField(rowObj, fieldName, value) { var newCell = rowObj.insertCell(0); fieldName = fieldName + '[]'; var inputTxt = '<input type="text" name="'+fieldName+'" value="'+value+'"'; inputTxt += ' readonly="readonly" style="background-color:#cecece;width:50px;" />'; inputObj = document.createElement(inputTxt); newCell.appendChild(inputObj); return inputObj; } </script> <form name="input_form"> <table> <tr> <th>Qty</th> <th>Product</th> <th>Item</th> <th>Unit Price</th> <th>Qty Price</th> <th> </th> </tr> <tr> <td><input name="quantity" type="text" size="6" value="1"/></td> <td> <select name="product" onchange="this.style.backgroundColor = '#FFCCCC'"> <option value=""> </option> <option value="1">add 1</option> <option value="2">add 2</option> <option value="3">add 3</option> <option value="4">add 4</option> <option value=""> </option> <option value="450">Nokia</option> </select> </td> <td> <select name="itemComp" onchange="this.style.backgroundColor = '#FFCCCC'"> <option value=""></option> <option value="340">Orange</option> </select> </td> <td><input name="unitPrice" type="text" size="14" value="0"/></td> <td><input name="qtyPrice" type="text" size="14" value="0"/></td> <td><button onclick="addComission()">Add Comission</button></td> </tr> </table> </form> <br><br>Commisions: <form name="commissioForm"> <table id="commissionTable"> <tr> <th>Qty</th> <th>Product</th> <th>Item</th> <th>Unit Price</th> <th>Qty Price</th> <th>GP</th> <th> </th> </tr> </table> <button type="submit" id="submit">Submit Commisions</button> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.