Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 17, 2023 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/e688271a164057b65661100c4076ee9e to your computer and use it in GitHub Desktop.
Save code-boxx/e688271a164057b65661100c4076ee9e to your computer and use it in GitHub Desktop.
Javascript Seat Reservation

JAVASCRIPT SEAT RESERVATION

https://code-boxx.com/seat-reservation-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Seat Reservation</title>
<meta charset="utf-8">
<script src="2-seat-reserve.js"></script>
<link rel="stylesheet" href="3-seat-reserve.css">
</head>
<body>
<!-- (A) SEAT LAYOUT -->
<div id="layout"></div>
<!-- (B) LEGEND -->
<div id="legend">
<div class="seat"></div> <div class="txt">Available</div>
<div class="seat taken"></div> <div class="txt">Taken</div>
<div class="seat selected"></div> <div class="txt">Your Chosen Seats</div>
</div>
<!-- (C) SAVE SELECTION -->
<button id="save" onclick="reserve.save()">Reserve Seats</button>
</body>
</html>
var reserve = {
// (A) INIT
init : () => {
// (A1) GET LAYOUT WRAPPER
let layout = document.getElementById("layout");
// (A2) GENERATE SEATS
for (let i=65; i<=68; i++) { for (let j=1; j<=4; j++) {
let seat = document.createElement("div");
seat.innerHTML = String.fromCharCode(i) + j;
seat.className = "seat";
seat.onclick = () => reserve.toggle(seat);
layout.appendChild(seat);
}}
// (A3) FOR DEMO ONLY - RANDOM TAKEN SEATS
let all = document.querySelectorAll("#layout .seat"),
len = all.length - 1, rnd = [];
while (rnd.length != 3) {
let r = Math.floor(Math.random() * len);
if (!rnd.includes(r)) { rnd.push(r); }
}
for (let i of rnd) {
all[i].classList.add("taken");
all[i].onclick = "";
}
},
// (B) CHOOSE THIS SEAT
toggle : seat => seat.classList.toggle("selected"),
// (C) SAVE RESERVATION
save : () => {
// (C1) GET SELECTED SEATS
let selected = document.querySelectorAll("#layout .selected");
// (C2) ERROR!
if (selected.length == 0) { alert("No seats selected."); }
// (C3) SELECTED SEATS
else {
// (C3-1) GET SELECTED SEAT NUMBERS
let seats = [];
for (let s of selected) { seats.push(s.innerHTML); }
// (C3-2) DO SOMETHING WITH IT...
let data = new FormData();
data.append("seats", JSON.stringify(seats));
data.append("name", "JON DOE");
data.append("email", "JON@DOE.COM");
fetch("4-dummy.php", {
method: "POST",
body : data
})
.then(res => res.text())
.then(txt => console.log(txt));
}
}
};
window.addEventListener("DOMContentLoaded", reserve.init);
/* (A) SEAT & "COLOR CODE" */
.seat {
text-align: center;
padding: 20px 10px;
border-radius: 10px;
background: #f1f1f1;
}
.taken { background: #df0000; color: #fff; }
.selected { background: #87ff96; }
/* (B) SEATS LAYOUT */
#layout {
max-width: 400px;
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 10px;
margin-bottom: 20px;
}
/* (C) LEGEND */
#legend {
display: grid;
grid-gap: 10px;
grid-template-columns: 50px auto ;
}
#legend .txt {
display: flex;
align-items: center;
}
/* (D) SAVE */
#save {
font-size: 20px;
margin-top: 20px;
padding: 10px 20px;
border: 0;
color: #fff;
background: #00479f;
}
/* (X) DOES NOT MATTER */
* {
font-family: arial, sans-serif;
box-sizing: border-box;
}
<?php
print_r($_POST);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment