I'm currently working on a project for my digital class and I'm facing an issue with my booking form. I have two functions that need to execute when a button is clicked - one function validates the form to ensure all necessary fields are filled out, while the other runs the main loop. Despite looking through various resources online, I haven't been able to figure it out yet.
I attempted adding the validForm function within the main loop so that it runs first before the loop. Then, I tried running just the main loop without the validForm function, but it ran the loop first and then checked for the required forms (I actually need it to check for required forms first and then run the loop). It's proving to be quite challenging for me.
--- condensed code snippet (html) ---
<form id="thisForm" method="post" name="thisForm" onsubmit="validForm()">
<label>First name:</label> <input autocomplete="on" id="firstNameInput" placeholder="John" required="" title="Enter your first name" type="text"><br>
<label>Last name:</label> <input autocomplete="on" id="lastNameInput" placeholder="Smith" required="" title="Enter your last name" type="text"><br>
<label>Age:</label> <input autocomplete="on" id="ageInput" placeholder="18" required="" title="Enter your age" type="number"><br>
<label>Email address:</label> <input autocomplete="on" id="emailInput" placeholder="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="80eaefe8eef3ede9f4e8c0e7ede1e9ecaee3efed">[email protected]</a>" required="" title="Enter your email address" type="email">
<button class="button button1" id="submit" onsubmit="loopForm(form)">Confirm Booking</button>
<button class="button button2" id="reset" onclick="index.html">Reset</button>
</form>
--- condensed javascript logic ---
function validForm() {
var x = document.forms["myForm"]["numberOfDays"].value;
if (x == "") {
alert("Name must be filled out");
return false;
}
}
function loopForm(form) { // Function executed upon clicking confirm button to send data to firebase
alert('Test alert for confirm booking');
var amountOfDays = numberOfDays.value;
var insuranceFee = 20;
var BOOKINGFEE = 50;
var sum = 0 ;
var cbResults = ' ';
for (var i = 0; i < form.elements.length; i++) {
if (form.elements[i].type == 'radio') {
if (form.elements[i].checked == true) {
vehicleResult = form.elements[i].value;
vehicleCost = form.elements[i].dataset.price;
insuranceCost = Math.round(insuranceFee + vehicleCost * amountOfDays);
outputDays.innerHTML = amountOfDays;
outputTest.innerHTML = insuranceCost;
outputVehicle.innerHTML = vehicleResult;
}
}
if (form.elements[i].type == "checkbox") {
if (form.elements[i].checked == true) {
cbResults += form.elements[i].value + ', ';
sum = sum + parseInt(form.elements[i].dataset.price);
alert(cbResults + "$" + sum);
outputExtras.innerHTML = cbResults;
totalCost = Math.round(insuranceCost + sum + BOOKINGFEE);
outputCost.innerHTML = '$' + totalCost;
}
}
}
}
I am anticipating the output to lead to a booking summary page instead of refreshing once the page checks for required fields. Currently, the page refreshes before I get to view the booking summary.