1 year ago
#346389
idknmre
Multipage form to show next div class after hitting submit in Javascript?
I am trying to create a flight reservation program that asks users to input their details. When they click 'Reserve flight', they are then shown a receipt on which their details are printed.
However, this is my javascript code that is supposed to show the next . The function showTab works correctly but I cannot figure out what to do with function submit(n).
var currentTab = 0;
showTab(currentTab);
function showTab(n) {
var x = document.getElementsByClassName("tab");
x[n].style.display = "block";
if (n == (x.length - 1)) {
document.getElementById("nextBtn").innerHTML = "Reserve Flight";
} else {
document.getElementById("nextBtn").innerHTML = "Reserve Flight";
}
fixStepIndicator(n)
}
function submit(n) {
var x = document.getElementsByClassName("tab");
if (n == 1 && !validateForm()) return false;
x[currentTab].style.display = "none";
currentTab = currentTab + n;
if (currentTab >= x.length) {
document.getElementById("regForm").submit();
return false;
}
showTab(currentTab);
}
function validateForm() {
var x, y, i, valid = true;
x = document.getElementsByClassName("tab");
y = x[currentTab].getElementsByTagName("input");
for (i = 0; i < y.length; i++) {
if (y[i].value == "") {
y[i].className += " invalid";
valid = false;
}
}
}
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap');
*{
margin: 10;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins',sans-serif;
}
body{
font-family: sans-serif;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
padding: 10px;
background: #ededed;
}
.container{
top: 100px;
max-width: 700px;
width: 100%;
background-color: #fff;
padding: 25px 30px;
border-radius: 5px;
box-shadow: 0 5px 10px rgba(0,0,0,0.15);
}
.container .title{
font-size: 25px;
font-weight: 500;
position: relative;
}
/*Input Box Sizes*/
.content form .user-details{
display: flex;
flex-wrap: wrap;
justify-content: space-between;
margin: 20px 0 12px 0;
}
form .user-details .input-box{
margin-bottom: 15px;
width: calc(100% / 3 - 20px);
}
form .input-box span.details{
display: block;
font-weight: 100;
margin-bottom: 5px;
font-size: 12px;
}
.user-details .input-box input{
height: 40px;
width: 100%;
outline: none;
font-size: 15px;
border-radius: 3px;
padding-left: 15px;
border: 1px solid #ccc;
transition: all 0.3s ease;
}
form .button{
height: 45px;
margin: 35px 0;
}
.next-btn{
height: 200%;
width: 150%;
margin-left: 400%;
border-radius: 5px;
border: none;
color: #fff;
font-size: 15px;
font-weight: 300;
letter-spacing: 1px;
cursor: pointer;
transition: all 0.3s ease;
background: #2e69ff;
display: inline-block;
}
form .next-btn input:hover{
/* transform: scale(0.99); */
background: #193f9e;
}
And my HTML
<body>
<div class="container">
<div class="tab">
<div class="title">Flight Regsitration</div>
<br><br>
<div class="content">
<form id="regForm">
<span class="details">Passenger Name</span>
<div class="user-details">
<div class="input-box">
<input type="text" placeholder="" id="nameTitle">
<span class="details">Title</span>
</div>
<div class="input-box">
<input type="text" placeholder="" id="nameFirst" required>
<span class="details">First Name</span>
</div>
<div class="input-box">
<input type="text" placeholder="" id="nameLast" required>
<span class="details">Last Name</span>
</div>
</div>
<div>
<button type="submit" class="next-btn" id="nextBtn">Reserve Flight</button>
</div>
</form>
</div>
</div>
<div class="tab">
<div class="title">Itinerary Receipt</div>
<br><br>
<table class="table" id="receipt">
<tr>
<th style="background-color: #dddddd;">Passenger Details</th>
<th style="background-color: #dddddd;"></th>
</tr>
<tr>
<td>Name Title</td>
<td>Insert user input here</td>
</tr>
<tr>
<td>First Name</td>
<td>Insert user input here</td>
</tr>
<tr>
<td>Last Name</td>
<td>Insert user input here</td>
</tr>
<table>
<br><br>
<p>Thank you for patronizing our services.<p>
</div>
<script src="javascript-multistep.js"></script>
</div>
</body>
javascript
html
css
user-registration
multipage
0 Answers
Your Answer