Creating a simple JavaScript program to store personal information in a database and allow basic operations like adding, searching, displaying, and deleting records, can be done using web storage for simplicity. In a real-world application, you would usually have a backend service with a database to handle these operations. For demonstration purposes, I'll show you how to implement this using browser's localStorage.
Here's a simple program:
<!DOCTYPE html>
<html>
<head>
<title>Simple Database</title>
<script>
// Function to load data from localStorage
function loadData() {
const data = JSON.parse(localStorage.getItem('records')) || [];
return data;
}
// Function to save data to localStorage
function saveData(data) {
localStorage.setItem('records', JSON.stringify(data));
}
// Function to add a new record
function addRecord() {
const name = document.getElementById('name').value;
const fatherName = document.getElementById('fatherName').value;
const nationalNumber = document.getElementById('nationalNumber').value;
const dob = document.getElementById('dob').value;
const qualification = document.getElementById('qualification').value;
const phoneNumber = document.getElementById('phoneNumber').value;
const position = document.getElementById('position').value;
const address = document.getElementById('address').value;
const records = loadData();
records.push({ name, fatherName, nationalNumber, dob, qualification, phoneNumber, position, address });
saveData(records);
displayData();
}
// Function to display records in the HTML table
function displayData() {
const records = loadData();
const tableBody = document.getElementById('recordsTable').getElementsByTagName('tbody')[0];
tableBody.innerHTML = '';
records.forEach((record, index) => {
const row = tableBody.insertRow();
Object.values(record).forEach(value => {
const cell = row.insertCell();
cell.textContent = value;
});
// Add delete button
const deleteCell = row.insertCell();
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.onclick = () => deleteRecord(index);
deleteCell.appendChild(deleteButton);
});
}
// Function to delete a record
function deleteRecord(index) {
let records = loadData();
records.splice(index, 1);
saveData(records);
displayData();
}
// Function to search records based on input
function searchRecords() {
const searchTerm = document.getElementById('searchTerm').value.toLowerCase();
const records = loadData();
const results = records.filter(record =>
Object.values(record).some(value => value.toLowerCase().includes(searchTerm))
);
const tableBody = document.getElementById('recordsTable').getElementsByTagName('tbody')[0];
tableBody.innerHTML = '';
results.forEach((record, index) => {
const row = tableBody.insertRow();
Object.values(record).forEach(value => {
const cell = row.insertCell();
cell.textContent = value;
});
// Add delete button for search results
const deleteCell = row.insertCell();
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.onclick = () => deleteRecord(index);
deleteCell.appendChild(deleteButton);
});
}
document.addEventListener('DOMContentLoaded', displayData);
</script>
</head>
<body>
<h2>Personal Information Database</h2>
<div>
<label for="name">Name:</label>
<input type="text" id="name">
<br>
<label for="fatherName">Father's Name:</label>
<input type="text" id="fatherName">
<br>
<label for="nationalNumber">National Number:</label>
<input type="text" id="nationalNumber">
<br>
<label for="dob">Date of Birth:</label>
<input type="date" id="dob">
<br>
<label for="qualification">Educational Qualification:</label>
<input type="text" id="qualification">
<br>
<label for="phoneNumber">Phone Number:</label>
<input type="text" id="phoneNumber">
<br>
<label for="position">Position:</label>
<input type="text" id="position">
<br>
<label for="address">Address:</label>
<input type="text" id="address">
<br>
<button onclick="addRecord()">Add Record</button>
</div>
<div>
<h3>Search Records</h3>
<input type="text" id="searchTerm" placeholder="Search here...">
<button onclick="searchRecords()">Search</button>
</div>
<h3>Records</h3>
<table id="recordsTable" border="1">
<thead>
<tr>
<th>Name</th>
<th>Father's Name</th>
<th>National Number</th>
<th>Date of Birth</th>
<th>Educational Qualification</th>
<th>Phone Number</th>
<th>Position</th>
<th>Address</th>
<th>Action</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
Key Features:
- Add New Records: Users can add new personal information records.
- Display Records: All stored records are displayed in a table format.
- Delete Records: Each record has an option to be deleted.
- Search Functionality: Users can search through records based on any detail they have entered, thanks to the filtering function.
Note:
- This program is a simple demonstration and uses
localStorage which is not a substitute for a real database. For production purposes, you would implement server-side database handling with backend technologies like Node.js with databases like MongoDB, SQL, etc.
localStorage has size limitations and is not suitable for storing sensitive data.