Giter Club home page Giter Club logo

hm05 / formula Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 762 KB

Formula is a nodejs web application which is used for form submissions. This concept is similar to google forms. The technologies used to make this web application are HTML5, CSS3, Bootstrap, JavaScript and NodeJS. I hope you find this repository informative.

Home Page: https://hm05.github.io/Formula/

HTML 47.08% CSS 5.78% JavaScript 47.14%
css3 html5 nodejs bootstrap jquery-ajax

formula's Introduction

Formula

Form submission faster than Formula racing

Description

Formula is a nodejs web application which is used for form submissions. This concept is similar to google forms. The technologies used to make this web application are HTML5, CSS3, Bootstrap, JavaScript and NodeJS. I hope you find this repository informative.

Installation

Here is the step by step installation process of this project

  1. Cloning the repository
git clone https://github.com/hm05/Formula.git && cd ./Formula
  1. Verifying NodeJS on local system

Important

This project uses NodeJS as back-end so its crutial to have NodeJS installed on your local system

To check for NodeJS use the below command, if you have Node already installed the command will return with the version of the Node but if you don't have Node then it will return with node command unidentified or similar error.

node version
  1. Installing dependencies

Tip

I have already included node modules in the repository but it is advisable to install the dependencies

Run the below command to install the dependencies

npm install express body-parser cors
  1. Running the Back-End
node server.js

After running the backend server your application is ready to run but please use different port for front-end

  1. Running the Front-End

Tip

I would recommend you to use Live Server Extension if you are using Visual Studio Code as this extension uses 5500 port to host Front-End

  1. Accessing the Web-Application Now as you have also set-up the Web Application you can access the application through localhost
http://127.0.0.1:5500/index.html

Explaination

  • Here we will understand the working of code in the flow of data
  • Here I won't go much deeper into the structure and styling of the page but rather we will focus on the frameworks and functions used in html

Here I have used bootstrap framework for styling which can be understood by the following lines of code

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

Then we can understand that I am using JQuery AJAX for the transfer of data from the below code

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

Now let us understand the working of the submit button

<div class="submit-btn">
        <button type="button" class="btn btn-primary" onclick="submitForm()">Submit</button>
</div>

Here we are calling a function called "submitForm()" on clicking the button from main.js file.

  • Here let us understand the submitForm() function
function submitForm() {
  const name = document.getElementById("exampleInputName").value;
  const mobile = document.getElementById("exampleInputMobile").value;
  const studentID = document.getElementById("exampleInputStudentID").value;
  const university = document.getElementById("exampleInputUniversity").value;
  const otherUniversity = document.getElementById("otherUniversityInput").value;

  const formData = {
    name,
    mobile,
    studentID,
    university: university === "Other" ? otherUniversity : university
  };

  // Send form data to the server
  $.ajax({
    url: "http://localhost:3000/submit-form",
    type: "POST",
    contentType: "application/json",
    data: JSON.stringify(formData),
    success: function(response) {
      console.log("Form submitted successfully:", response);
      // Optionally, display a success message to the user
    },
    error: function(xhr, status, error) {
      console.error("Failed to submit form:", error);
      // Optionally, display an error message to the user
    }
  });
}

From this can understand that this function is using a POST method to send data to server.js on port 3000 using AJAX and on successful submission of form this function is responding "Form submitted successfully" in the console logs. And on error it is responding with "Failed to submit form" in console log.

app.post('/submit-form', (req, res) => {
  const formData = req.body;
  console.log('Form Data:', formData);
  
  // Read existing data from data.json file
  let jsonData = [];
  try {
    const data = fs.readFileSync('data.json', 'utf8');
    jsonData = JSON.parse(data);
  } catch (err) {
    console.error('Error reading data.json file:', err);
  }

  // Append new form data to jsonData array
  jsonData.push(formData);

  // Write updated data back to data.json file
  fs.writeFile('data.json', JSON.stringify(jsonData, null, 2), (err) => {
    if (err) {
      console.error('Error writing to data.json file:', err);
      res.status(500).json({ message: 'Failed to submit form' });
    } else {
      console.log('Form data submitted successfully');
      res.json({ message: 'Form submitted successfully' });
    }
  });
});

From the above code we can understand that after recieving the data from main.js the data is jsonified and then appended into data.json file.

Contributors

formula's People

Contributors

hm05 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.