Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/form data export #48

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions forms-portal/user-registration-form/formHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// formHandler.js

document.addEventListener("DOMContentLoaded", () => {
const form = document.querySelector('.user-registration-form');

// Add an event listener to handle form submission
form.addEventListener('submit', function (event) {
event.preventDefault(); // Prevents the form from submitting and refreshing the page

// Get the values from the form fields
const fullName = form.querySelector('input[placeholder="Full Name"]').value;
const phoneNumber = form.querySelector('input[placeholder="Phone Number"]').value;
const emailID = form.querySelector('input[placeholder="Email ID"]').value;
const comments = form.querySelector('textarea[placeholder="Comments"]').value;

// Log the data to the console
console.log(`Full Name: ${fullName}`);
console.log(`Phone Number: ${phoneNumber}`);
console.log(`Email ID: ${emailID}`);
console.log(`Comments: ${comments}`);

// Optionally, you can show a confirmation message or reset the form
alert('Form submitted successfully!');
form.reset(); // Reset the form fields after submission
});
});

document.addEventListener('DOMContentLoaded', () => {
const form = document.querySelector('.user-registration-form');

// Handle form submission
form.addEventListener('submit', (e) => {
e.preventDefault();

// Collect form data
const formData = {
fullName: form.querySelector('input[placeholder="Full Name"]').value,
phoneNumber: form.querySelector('input[placeholder="Phone Number"]').value,
emailId: form.querySelector('input[placeholder="Email ID"]').value,
comments: form.querySelector('textarea[placeholder="Comments"]').value,
};

console.log(formData); // Log the form data in the console

// Export options
const exportType = prompt("Type 'CSV' for CSV export or 'EXCEL' for Excel export:");

if (exportType.toLowerCase() === 'csv') {
exportToCSV(formData);
} else if (exportType.toLowerCase() === 'excel') {
exportToExcel(formData);
} else {
alert("Invalid option selected.");
}

// Reset form
form.reset();
});

// Function to export data to CSV
function exportToCSV(data) {
const csvContent = `Full Name,Phone Number,Email ID,Comments\n${data.fullName},${data.phoneNumber},${data.emailId},${data.comments}`;
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const link = document.createElement('a');
const url = URL.createObjectURL(blob);
link.setAttribute('href', url);
link.setAttribute('download', 'form_data.csv');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}

// Function to export data to Excel
function exportToExcel(data) {
const workbook = XLSX.utils.book_new();
const worksheetData = [['Full Name', 'Phone Number', 'Email ID', 'Comments'],
[data.fullName, data.phoneNumber, data.emailId, data.comments]];

const worksheet = XLSX.utils.aoa_to_sheet(worksheetData);
XLSX.utils.book_append_sheet(workbook, worksheet, 'Form Responses');

XLSX.writeFile(workbook, 'form_data.xlsx');
}
});
42 changes: 21 additions & 21 deletions forms-portal/user-registration-form/userRegistrationForm.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" href="style.css" />
</head>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" href="style.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>

<body>
<div class="user-registration-form-container">
</head>
<body>
<div class="user-registration-form-container">
<h1>User Registration Form</h1>
<form class="user-registration-form">
<input type="text" placeholder="Full Name" />
<input type="number" placeholder="Phone Number" />
<input type="text" placeholder="Email ID" />
<textarea placeholder="Comments"></textarea>
<button>Submit</button>
</form>
</div>
<script src="./formHandler.js"></script>
</body>
</html>

<h1>User Registration Form</h1>

<form class="user-registration-form">
<input type="text" placeholder="Full Name" />
<input type="number" placeholder="Phone Number" />
<input type="text" placeholder="Email ID" />
<textarea placeholder="Comments"></textarea>
<button>Submit</button>
</form>
</div>
</body>

</html>
Loading