Creating a simple web application to collect user information and save it to a text file requires both frontend (HTML) and backend logic. However, HTML itself is not capable of file operations, so we need a backend language like Python (Flask), Node.js, PHP, or others to handle file operations.
Below is an outline using Node.js and Express to create a backend server. The frontend will be in HTML, and we'll use JavaScript (fetch API) to communicate with the backend. We'll assume a basic understanding of Node.js and Express setup.
Step 1: Set up the Project
-
Initialize your Node.js project:
mkdir user-info-app
cd user-info-app
npm init -y
npm install express body-parser cors
-
Create the necessary files:
touch app.js data.txt
mkdir public
touch public/index.html
Step 2: Backend (Node.js)
app.js:
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const app = express();
const PORT = 3000;
app.use(bodyParser.json());
app.use(express.static('public'));
// Endpoint to add a new record
app.post('/add', (req, res) => {
const record = req.body;
fs.appendFile('data.txt', JSON.stringify(record) + '\n', (err) => {
if (err) {
return res.status(500).send('Error saving data');
}
res.status(200).send('Data saved successfully');
});
});
// Endpoint to get all records
app.get('/records', (req, res) => {
fs.readFile('data.txt', 'utf-8', (err, data) => {
if (err) {
return res.status(500).send('Error reading data');
}
const records = data.trim().split('\n').map(JSON.parse);
res.json(records);
});
});
// Endpoint to delete a specific record
app.delete('/delete', (req, res) => {
const { nationalNumber } = req.body;
fs.readFile('data.txt', 'utf-8', (err, data) => {
if (err) {
return res.status(500).send('Error reading data');
}
const records = data.trim().split('\n').map(JSON.parse);
const newRecords = records.filter(record => record.nationalNumber !== nationalNumber);
fs.writeFile('data.txt', newRecords.map(r => JSON.stringify(r)).join('\n') + '\n', (err) => {
if (err) {
return res.status(500).send('Error deleting data');
}
res.status(200).send('Record deleted successfully');
});
});
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Step 3: Frontend (HTML & JavaScript)
public/index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Information Form</title>
</head>
<body>
<h1>User Information Form</h1>
<form id="userForm">
<input type="text" name="name" placeholder="Name" required><br>
<input type="text" name="fathersName" placeholder="Father's Name" required><br>
<input type="text" name="nationalNumber" placeholder="National Number" required><br>
<input type="date" name="dob" required><br>
<input type="text" name="qualification" placeholder="Qualification" required><br>
<input type="tel" name="telephone" placeholder="Telephone" required><br>
<input type="text" name="position" placeholder="Position" required><br>
<textarea name="address" placeholder="Address" required></textarea><br>
<button type="submit">Submit</button>
</form>
<h2>Records</h2>
<div id="records"></div>
<script>
document.getElementById('userForm').addEventListener('submit', function(event) {
event.preventDefault();
const formData = new FormData(this);
const data = Object.fromEntries(formData);
fetch('/add', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => response.text())
.then(data => {
alert(data);
fetchRecords(); // Refresh the records after adding
});
});
function fetchRecords() {
fetch('/records')
.then(response => response.json())
.then(records => {
const recordDiv = document.getElementById('records');
recordDiv.innerHTML = records.map(record => `<div>
${JSON.stringify(record)}
<button onclick="deleteRecord('${record.nationalNumber}')">Delete</button>
</div>`).join('');
});
}
function deleteRecord(nationalNumber) {
fetch('/delete', {
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ nationalNumber })
})
.then(response => response.text())
.then(data => {
alert(data);
fetchRecords(); // Refresh the records after deletion
});
}
// Initial fetch of records when the page loads
fetchRecords();
</script>
</body>
</html>
Running the Application
-
Start your server with:
node app.js
-
Open your browser and navigate to http://localhost:3000 to use the application.
Note
- This example stores data in a plain text file, which is not efficient or secure for production use.
- Consider a database (such as SQLite, MongoDB) for storing data in real applications.
- Add more input validation and error handling as needed.