buildmy401k Posted July 31, 2015 Report Share Posted July 31, 2015 Hello I am trying to perform two calculations and autosumbit the page, however only the second calculation is being performed when the page is autosubmitted. Please advise. <SCRIPT LANGUAGE="JavaScript"> function calculate() { var v_Current_Plan = parseFloat(document.getElementById("InsertRecordCurrent_Plan").value); var v_Current_Fee = parseFloat(document.getElementById("InsertRecordCurrent_Fee").value); var v_Rate= .08; var v_total = (((v_Current_Plan * v_Rate) + v_Current_Plan) - (((v_Current_Plan * v_Rate) + v_Current_Plan) * v_Current_Fee)); document.getElementById("InsertRecordCurrent_Plan").value = Math.round(v_total); } { var v_New_Plan = parseFloat(document.getElementById("InsertRecordNew_Plan").value); var v_New_Fee = parseFloat(document.getElementById("InsertRecordNew_Fee").value); var v_Rate= .08; var v_total = (((v_New_Plan * v_Rate) + v_New_Plan) - (((v_New_Plan * v_Rate) + v_New_Plan) * v_New_Fee)); document.getElementById("InsertRecordNew_Plan").value = Math.round(v_total); } /* On submitting the webform, the function calculate is executed */ document.getElementById("caspioform").onsubmit=calculate; </SCRIPT> <SCRIPT LANGUAGE="JavaScript"> function calculate() { var v_Current_Plan = parseFloat(document.getElementById("InsertRecordCurrent_Plan").value); var v_Current_Fee = parseFloat(document.getElementById("InsertRecordCurrent_Fee").value); var v_Rate= .08; var v_total = (((v_Current_Plan * v_Rate) + v_Current_Plan) - (((v_Current_Plan * v_Rate) + v_Current_Plan) * v_Current_Fee)); document.getElementById("InsertRecordCurrent_Plan").value = Math.round(v_total); } { var v_New_Plan = parseFloat(document.getElementById("InsertRecordNew_Plan").value); var v_New_Fee = parseFloat(document.getElementById("InsertRecordNew_Fee").value); var v_Rate= .08; var v_total = (((v_New_Plan * v_Rate) + v_New_Plan) - (((v_New_Plan * v_Rate) + v_New_Plan) * v_New_Fee)); document.getElementById("InsertRecordNew_Plan").value = Math.round(v_total); } /* On submitting the webform, the function calculate is executed */ document.getElementById("caspioform").onsubmit=calculate; </SCRIPT> <script> if(document.getElementById("caspioform")) { document.getElementById("caspioform").style.display = 'none'; setTimeout('document.forms["caspioform"].submit()',0); } </script> Quote Link to comment Share on other sites More sharing options...
Xiang Posted August 3, 2015 Report Share Posted August 3, 2015 Good morning Allen, How are you doing? In Java Script, "{" means "Start" and "}" means "Finish". So, if you want to do several actions in a function, I think, it's better don't write "Finish" and then "Start" again between them, the function doesn't execute any code after the "Finish" sign. In setTimeout('document.forms["caspioform"].submit()',0); }, "0" means how long a DataPage waits before auto submits. It's better to use "1000", at least "100" milliseconds, then you allow a DataPage to load and then autosubmit it. And if you want to call another function before autosubmit, you can write the same code but for another function: setTimeout('calculate()',500); } Then the whole code will be like: <SCRIPT LANGUAGE="JavaScript"> function calculate() { var v_Current_Plan = parseFloat(document.getElementById("InsertRecordCurrent_Plan").value); var v_Current_Fee = parseFloat(document.getElementById("InsertRecordCurrent_Fee").value); var v_Rate= .08; var v_total = (((v_Current_Plan * v_Rate) + v_Current_Plan) - (((v_Current_Plan * v_Rate) + v_Current_Plan) * v_Current_Fee)); document.getElementById("InsertRecordCurrent_Plan").value = Math.round(v_total); var v_New_Plan = parseFloat(document.getElementById("InsertRecordNew_Plan").value); var v_New_Fee = parseFloat(document.getElementById("InsertRecordNew_Fee").value); var v_Rate= .08; var v_total = (((v_New_Plan * v_Rate) + v_New_Plan) - (((v_New_Plan * v_Rate) + v_New_Plan) * v_New_Fee)); document.getElementById("InsertRecordNew_Plan").value = Math.round(v_total); } if(document.getElementById("caspioform")) { document.getElementById("caspioform").style.display = 'none'; setTimeout('calculate()',500); setTimeout('document.forms["caspioform"].submit()',1000); } </script> I'm learning the JavaScript now, so, I'll be grateful, if you tell me if the code works. Have a nice day! buildmy401k 1 Quote Link to comment Share on other sites More sharing options...
buildmy401k Posted August 3, 2015 Author Report Share Posted August 3, 2015 Xiang, Thank you so much for the help. Your JS worked perfectly! 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.
Note: Your post will require moderator approval before it will be visible.