I have the declaration form sample where the name and designation value alone should be fetched from
MS SQL Database for each user.So this has to be dynamically fetched on click of the name in a row.
I need the full code step by step as am a beginer.Thank you in advance
Loading

Deepak RawatRank #646 · 3.7K reputationPosted May 11, 2023, 4:05 AM EDT
Hi, you can try this but Please note that this code assumes you are using a server-side technology like Node.js and have set up a connection to your MS SQL Database.
Step 1: Set up the HTML structure: Create an HTML table with rows and columns to display the user information, and include a click event handler on the name column.
Step 2: Create the JavaScript code (script.js): In the script.js file, define the
fetchUserInfofunction that will handle the click event and fetch the name and designation values from the database.function fetchUserInfo(userId) { // Make a request to the server to fetch user info based on the userId fetch(`/users/${userId}`) .then(response => response.json()) .then(data => { // Update the corresponding designation column with the fetched value const designationColumn = document.querySelector(`tr:nth-child(${userId}) td:nth-child(2)`); designationColumn.textContent = data.designation; }) .catch(error => console.error(error)); }Step 3: Set up the server-side code (assuming Node.js and Express.js): Create a server-side route that handles the request to fetch user information from the MS SQL Database.
const express = require('express'); const app = express(); const sql = require('mssql'); // Configure your database connection const config = { user: 'your-username', password: 'your-password', server: 'your-server', database: 'your-database' }; // Route to fetch user information based on the userId app.get('/users/:userId', (req, res) => { const userId = req.params.userId; // Connect to the database sql.connect(config, err => { if (err) { console.error(err); res.status(500).json({ error: 'Failed to connect to the database' }); return; } // Execute the SQL query to fetch user information new sql.Request().query(`SELECT Name, Designation FROM Users WHERE UserId = ${userId}`, (err, result) => { if (err) { console.error(err); res.status(500).json({ error: 'Failed to fetch user information' }); } else { // Send the fetched data as a response res.json(result.recordset[0]); } // Close the database connection sql.close(); }); }); }); // Start the server app.listen(3000, () => { console.log('Server started on port 3000'); });Make sure to install the necessary dependencies such as
expressandmssqlusing npm before running the server.