Ivonne Aspilcueta

Ivonne Aspilcueta

  • 882
  • 622
  • 10.2k

How to fill all input fields automatically from database

Oct 11 2024 7:10 PM

Hello,

How to fill all input fields automatically from database by entering input in one textbox using Javascript? Do I need a web services?

This is so far I create with help, but still cannot retrieve the full name and ssn from database when enter memberid in the input text.

2 erros is happening right now:

1) getUserDetails() is not deifned AND

2) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received

async function getUserDetails(MemberID) {
    try {
        const config = {
           
            server: '.',
            database: 'WebForms',
            options: {
                //encrypt: true // Use this if you're on Azure
            }
        };

        // Connect to the database
        await sql.connect(config);

        // Query the database
        const result = await sql.query`SELECT RTRIM(LTRIM(CONCAT(COALESCE(FirstName + ' ', ''), COALESCE(LastName, '')))) AS MemberName, SSN FROM dbo.Members WHERE MemberID = ${MemberID}`;

        if (result.recordset.length > 0) {
            const { MemberName, SSN } = result.recordset[0];
            console.log(`Name: ${MemberName} ${SSN}`);

           result = document.getElementById("membername").value="";
           result = document.getElementById("SSN").value="";

        } else {
            console.log('No user found with the provided account number.');
        }
    } catch (err) {
        console.error('SQL error', err);
    } finally {
        await sql.close();
    }
}

 


Answers (4)