0% found this document useful (0 votes)
14 views5 pages

Bus Booking Form and Process

The document consists of an HTML file for a bus booking form and a Node.js server script. The HTML form collects user information such as name, email, journey date, and destination, and sends it to the server for processing. The server connects to a MongoDB database to save booking data and responds with success or error messages based on the booking operation.

Uploaded by

moghana1025
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Bus Booking Form and Process

The document consists of an HTML file for a bus booking form and a Node.js server script. The HTML form collects user information such as name, email, journey date, and destination, and sends it to the server for processing. The server connects to a MongoDB database to save booking data and responds with success or error messages based on the booking operation.

Uploaded by

moghana1025
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Bus Booking html

[Link]

<!DOCTYPE html>

<html>

<head><title>

Bus Booking

</title></head>

<body>

<header>

<h1>Bus Booking Site</h1>

</header>

<main>

<form class="busBookingForm" action="/book-bus" method="POST">

<label for = "name">Name : </label>

<input type = "text" id="name" placeholder="Enter your name " name = "name"
required>

<label for = "email">Email : </label>

<input type = "email" id="email" placeholder = "Enter your email" name = "email"
required>

<label for = "journeyDate">Journey Date : </label>

<input type = "date" id="journeyDate" name="journeyDate" required>

<label for = "destination">Destination : </label>

<input type ="text" id ="destination" placeholder="Enter your destination"


name="destination">

<button type = "submit">Book now</button>

</form>

</main>
</body>

<script>

[Link]('form').addEventListener('submit', async (e) => {

[Link](); // Prevent default form submission

const formData = {

name: [Link]('name').value,

email: [Link]('email').value,

journeyDate: [Link]('journeyDate').value,

destination: [Link]('destination').value,

};

try {

const response = await fetch('/book-bus', {

method: 'POST',

headers: {

'Content-Type': 'application/json',

},

body: [Link](formData),

});

// Log the response for debugging

[Link]('Server Response:', response);

if ([Link]) {

const message = await [Link]();

[Link]('Message from server:', message); // Should be "Booking successful"


// Redirect to confirmation page

[Link] = '/[Link]';

} else {

const errorText = await [Link]();

[Link]('Error response from server:', errorText);

alert(`Error booking ticket: ${errorText}`);

} catch (error) {

[Link]('Error during booking:', error);

alert('Error booking ticket. Please check the console for details.');

});

</script>

</html>

[Link]

const express = require('express');

const mongoose = require('mongoose');

const bodyParser = require('body-parser');

const path = require('path');

const app = express();

const port = 3000;

// MongoDB connection

[Link]('mongodb://localhost:27017/ticketBooking', {

useNewUrlParser: true,
useUnifiedTopology: true,

});

const bookingSchema = new [Link]({

name: { type: String, required: true },

email: { type: String, required: true },

journeyDate: { type: String, required: true },

destination: { type: String, required: true },

});

const Booking = [Link]('Booking', bookingSchema);

// Middleware

[Link]([Link]());

[Link]([Link]({ extended: true })); // For form submissions

// Serve static files from the root directory

[Link]([Link]([Link](__dirname)));

// Routes

[Link]('/book-bus', async (req, res) => {

try {

const bookingData = [Link];

// Debug the incoming data

[Link]('Received booking data:', bookingData);

const booking = new Booking(bookingData);

await [Link]();

[Link](200).send('Booking successful');
} catch (err) {

[Link]('Error saving booking:', err);

[Link](500).send('Error booking ticket');

});

[Link]('connected', () => {

[Link]('Connected to MongoDB');

});

[Link]('error', (err) => {

[Link]('Error connecting to MongoDB:', err);

});

// Start server

[Link](port, () => {

[Link](`Server running at [Link]

});

You might also like