I have created a web form that has several input as well as radio buttons. The answer for each needs to be gathered to calculate a cost. I am not sure if my JS logic (especially related to the radio buttons) is correct. Can someone please take a look at this to see if I am doing this correctly? Thank you
The HTML:
<div class="row">
<div class="form-group col-md-4">
<label for="company">Company Name*</label><br />
<input type="text" class="form-control" name="company" placeholder="Company Name" ng-model="user.company">
</div>
<div class="form-group col-md-4">
<label for="salesp">Salesperson Name*</label><br />
<input type="text" class="form-control" name="salesp" placeholder="Salesperson Name" ng-model="user.salesp">
</div>
<div class="form-group col-md-4">
<label for="emailsales">Contact Email*</label><br />
<input type="email" class="form-control" name="emailsales" placeholder="[email protected]" ng-model="user.emailsales">
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="partnum">Number of Part Being Made(Finished Goods)</label>
<input type="number" class="form-control" id ="partnum" name="partnum" placeholder="Enter # items" ng-model="user.partnum">
</div>
<div class="form-group col-md-4">
<label>New Files?</label><br />
<input type="radio" name="files" id="files" value="42" ng-model="user.files"> Yes
<input type="radio" name="files" id="files" value="0"ng-model="user.files"> No
</div>
<div class="form-group col-md-4">
<label>Rerun Files?</label><br />
<input type="radio" name="oldfiles" value="15" ng-model="user.oldfiles"> Yes
<input type="radio" name="oldfiles" value="0" ng-model="user.oldfiles"> No
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="shortp">Shortest Dimension on Print Part (inches)</label>
<input type="number" class="form-control" name="shortp" placeholder="Shortest Dim" ng-model="user.shortp">
</div>
<div class="form-group col-md-4">
<label for="longp">Longest Dimension on Print Page (inches)</label>
<input type="number" class="form-control" name="longp" placeholder="Longest Dim" ng-model="user.longp">
</div>
<div class="form-group col-md-4">
<label for="pressnum">Number of Ups on Press Sheet</label>
<input type="number" class="form-control" name="pressnum" placeholder="# Ups" ng-model="user.pressnum">
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="shortpress">Number of Short Press Sheet</label>
<input type="number" class="form-control" id="shortpress" name="shortpress" placeholder="# Short Press" ng-model="user.shortpress">
</div>
<div class="form-group col-md-4">
<label for="longpress">Number of Long Press Sheet</label>
<input type="number" class="form-control" id="longpress" name="longpress" placeholder="# Long Press" ng-model="user.longpress">
</div>
<div class="form-group col-md-4">
<label>Passes First Side?</label><br />
<input type="radio" name="passfirst" value="Yes" ng-model="user.passfirst"> Yes
<input type="radio" name="passfirst" value="No" ng-model="user.passfirst"> No
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="inkcoverone">Percent of Ink Coverage on First Side</label>
<input type="number" class="form-control" id="inkcoverone" name="inkcoverone" placeholder="% Ink Coverage" ng-model="user.inkcoverone">
</div>
<div class="form-group col-md-4">
<label>Passes Second Side?</label><br />
<input type="radio" name="passsecond" value="1" ng-model="user.passsecond"> Yes
<input type="radio" name="passsecond" value="0" ng-model="user.passsecond"> No
</div>
<div class="form-group col-md-4">
<label for="inkcovertwo">Percent of Ink Coverage on Second Side</label>
<input type="number" class="form-control" name="inkcovertwo" placeholder="% Ink Coverage" ng-model="user.inkcovertwo">
</div>
</div>
<div class="row">
<div class="form-group col-md-offset-10">
<button type="submit" class="btn btn-default btn-block register" ng-click="quoteGenerator()"> GET QUOTE </button>
</div>
</div>
Here is the JS:
function quoteGenerator(){
let count = parseInt(document.getElementById('partnum').value);
if (count > 0) {
let size = 0;
for (var i = 0 ; i < document.getElementsByName('files').length; i++) {
if (document.getElementsByName('files')[i].checked) {
size = parseInt( document.getElementsByName('files')[i].value);
break;
}
if(size == 42){
let newcount = count * size;
} else if(size==0){
for (var i = 0 ; i < document.getElementsByName('oldfiles').length; i++) {
if (document.getElementsByName('oldfiles')[i].checked) {
newsize = parseInt( document.getElementsByName('oldfiles')[i].value);
break;
}
}
let newcount= count*newsize;
}
}
}
var ink = parseInt(document.getElementById('inkcoverone').value);
var shortp = parseInt(document.getElementById('shortpress').value);
var longp = parseInt(document.getElementById('longpress').value);
var parentsheet = parseInt(document.getElementById('psheetnum').value);
let inkcost1 = ((longp+shortp)/144)*.19*ink*parentsheet;
for (var i = 0 ; i < document.getElementsByName('passsecond').length; i++) {
if (document.getElementsByName('passsecond')[i].checked) {
pass2 = parseInt( document.getElementsByName('passsecond')[i].value);
break;
}
}
var ink2 = parseInt(document.getElementById('inkcovertwo').value);
let inkcost2 = ((longp+shortp)/144)*.19*ink2*parentsheet*pass2;
}
The equations are all based on the id of the variables in the HTML form. The equations are all correct, I am just not sure if the placement of the individual components for the totalCost are placed in the right place or not, nor am I sure about the logic of this calculation. The radio buttons caused me some problems.
I would recommend that you have a look at ng-model to learn how you can work with form fields.
Here is a demo app that I coded some time ago and should give you an idea how you could do calculations based on a form input.
Your quoteGenerator is a js function with-out any angular code. Inject $scope into the function or use controllerAs, create a controller and remove all document.getElementsById calls.
Here is your code were I've started to improve your code but I've stopped to complete the demo because I don't understand how the calculation with the files should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With