Creating a simple JavaScript program to handle user data involves setting up a basic form, storing the records in an array (or local storage if you want persistence), and implementing functions to display, search, and delete records. Below is an example of how you might structure such a program using HTML for the interface and JavaScript for the logic:
HTML
This provides the basic structure for input and display of data:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Records</title>
<style>
/* Basic styling for layout */
table { width: 100%; border-collapse: collapse; margin-top: 20px; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
</style>
</head>
<body>
<h2>User Records</h2>
<form id="userForm">
<input type="text" id="name" placeholder="Name" required>
<input type="text" id="fatherName" placeholder="Father's Name" required>
<input type="text" id="nationalNumber" placeholder="National Number" required>
<input type="date" id="dob" placeholder="Date of Birth" required>
<input type="text" id="qualification" placeholder="Educational Qualification" required>
<input type="tel" id="phoneNumber" placeholder="Phone Number" required>
<input type="text" id="position" placeholder="Position" required>
<input type="text" id="address" placeholder="Address" required>
<button type="submit">Save</button>
</form>
<h3>Database Records</h3>
<input type="text" id="searchQuery" placeholder="Search...">
<button onclick="searchRecords()">Search</button>
<button onclick="displayRecords()">Show All</button>
<table id="recordsTable">
<thead>
<tr>
<th>Name</th>
<th>Father's Name</th>
<th>National Number</th>
<th>Date of Birth</th>
<th>Qualification</th>
<th>Phone Number</th>
<th>Position</th>
<th>Address</th>
<th>Actions</th>
</tr>
</thead>
<tbody></tbody>
</table>
<script src="script.js"></script>
</body>
</html>
JavaScript (script.js)
This script handles the form submission, data storage, search, and deletion functionalities.
let records = [];
document.getElementById('userForm').addEventListener('submit', function(e) {
e.preventDefault();
const newRecord = {
name: document.getElementById('name').value,
fatherName: document.getElementById('fatherName').value,
nationalNumber: document.getElementById('nationalNumber').value,
dob: document.getElementById('dob').value,
qualification: document.getElementById('qualification').value,
phoneNumber: document.getElementById('phoneNumber').value,
position: document.getElementById('position').value,
address: document.getElementById('address').value,
};
records.push(newRecord);
displayRecords();
this.reset();
});
function displayRecords() {
const tableBody = document.querySelector('#recordsTable tbody');
tableBody.innerHTML = '';
records.forEach((record, index) => {
const row = document.createElement('tr');
Object.values(record).forEach(value => {
const cell = document.createElement('td');
cell.textContent = value;
row.appendChild(cell);
});
const deleteCell = document.createElement('td');
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.onclick = () => deleteRecord(index);
deleteCell.appendChild(deleteButton);
row.appendChild(deleteCell);
tableBody.appendChild(row);
});
}
function deleteRecord(index) {
records.splice(index, 1);
displayRecords();
}
function searchRecords() {
const query = document.getElementById('searchQuery').value.toLowerCase();
const filteredRecords = records.filter(record =>
Object.values(record).some(value =>
value.toLowerCase().includes(query)
)
);
displayFilteredRecords(filteredRecords);
}
function displayFilteredRecords(filteredRecords) {
const tableBody = document.querySelector('#recordsTable tbody');
tableBody.innerHTML = '';
filteredRecords.forEach((record, index) => {
const row = document.createElement('tr');
Object.values(record).forEach(value => {
const cell = document.createElement('td');
cell.textContent = value;
row.appendChild(cell);
});
const deleteCell = document.createElement('td');
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.onclick = () => deleteRecord(index);
deleteCell.appendChild(deleteButton);
row.appendChild(deleteCell);
tableBody.appendChild(row);
});
}
Description
- HTML Form: Facilitates input for user data.
- JavaScript: Handles interaction with form elements, data storage, rendering of records on the HTML table, searching, and deleting records.
- CSS: Offers basic table styling for clarity.
This standalone code snippet will execute in a web browser, ideally within an HTML file and a connected JavaScript file, allowing you to manage user records dynamically.