To address your requirements using JavaScript, we will create a simple web application. For storing the data, we will utilize localStorage as an in-browser database. This is fine for demonstration purposes and small-scale applications. For a production-level application, you should consider a more robust database solution like MySQL, MongoDB, or Firebase.
Here's a simple HTML and JavaScript implementation:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Database</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid black;
padding: 8px;
text-align: left;
}
</style>
</head>
<body>
<h1>Database Application</h1>
<form id="person-form">
<label for="fatherName">Father's Name:</label><br>
<input type="text" id="fatherName" name="fatherName"><br><br>
<label for="nationalNumber">National Number:</label><br>
<input type="text" id="nationalNumber" name="nationalNumber"><br><br>
<label for="dob">Date of Birth:</label><br>
<input type="date" id="dob" name="dob"><br><br>
<label for="education">Educational Qualification:</label><br>
<input type="text" id="education" name="education"><br><br>
<label for="telephone">Telephone Number:</label><br>
<input type="text" id="telephone" name="telephone"><br><br>
<label for="position">Position:</label><br>
<input type="text" id="position" name="position"><br><br>
<label for="address">Address:</label><br>
<input type="text" id="address" name="address"><br><br>
<button type="submit">Add Person</button>
</form>
<h2>Database</h2>
<table id="databaseTable">
<thead>
<tr>
<th>Father's Name</th>
<th>National Number</th>
<th>Date of Birth</th>
<th>Educational Qualification</th>
<th>Telephone</th>
<th>Position</th>
<th>Address</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script>
document.getElementById('person-form').addEventListener('submit', function (e) {
e.preventDefault();
const person = {
fatherName: document.getElementById('fatherName').value,
nationalNumber: document.getElementById('nationalNumber').value,
dob: document.getElementById('dob').value,
education: document.getElementById('education').value,
telephone: document.getElementById('telephone').value,
position: document.getElementById('position').value,
address: document.getElementById('address').value
};
let database = JSON.parse(localStorage.getItem('database')) || [];
database.push(person);
localStorage.setItem('database', JSON.stringify(database));
displayDatabase();
});
function displayDatabase() {
const tbody = document.getElementById('databaseTable').getElementsByTagName('tbody')[0];
tbody.innerHTML = '';
const database = JSON.parse(localStorage.getItem('database')) || [];
database.forEach(function (person, index) {
const row = tbody.insertRow();
row.insertCell().appendChild(document.createTextNode(person.fatherName));
row.insertCell().appendChild(document.createTextNode(person.nationalNumber));
row.insertCell().appendChild(document.createTextNode(person.dob));
row.insertCell().appendChild(document.createTextNode(person.education));
row.insertCell().appendChild(document.createTextNode(person.telephone));
row.insertCell().appendChild(document.createTextNode(person.position));
row.insertCell().appendChild(document.createTextNode(person.address));
const deleteCell = row.insertCell();
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.addEventListener('click', function () {
deletePerson(index);
});
deleteCell.appendChild(deleteButton);
});
}
function deletePerson(index) {
let database = JSON.parse(localStorage.getItem('database'));
database.splice(index, 1);
localStorage.setItem('database', JSON.stringify(database));
displayDatabase();
}
function searchDatabase(query) {
const database = JSON.parse(localStorage.getItem('database')) || [];
return database.filter(person =>
Object.values(person).some(value =>
value.toLowerCase().includes(query.toLowerCase())
)
);
}
// Initial load
displayDatabase();
</script>
</body>
</html>
Explanation:
-
HTML Form: A simple form is used to collect data such as father's name, national number, date of birth, educational qualification, telephone number, position, and address.
-
JavaScript Logic:
- When the form is submitted, it prevents the default form submission behavior and instead adds the entered data to an array stored in
localStorage.
- The
displayDatabase() function retrieves and visualizes stored data in a table format.
- Each entry in the database can be deleted using the "Delete" button next to it.
- A
searchDatabase() function is defined that would allow searching based on query through the database, although it's not hooked up to the UI here for clarity, it can be called to filter the results.
This example serves as a foundation, and you can expand upon it by adding a search feature UI, additional fields, validation, and more robust data handling as needed.