Should/Can I Query a MySQL database in a .ejs file?

Viewed 13

Essentially, I want to do a login in my Index page, and prevent the form from submitting if the details are wrong.

Eg.

function verify(){
user = document.getElementById("username").value;
password = document.getElementById("password").value;

// connect to database

// query table and compare user and password to existing values

// return true if match, otherwise false

}

and later

<input type="submit" value="Submit" onclick="return verify()">

However, I can't do that because I cannot query the database from the .ejs file.

I understand a question like this is answered here, which is to pass the data to that .ejs file from the corresponding .js file instead, but I doubt that is the best way to approach this, as it would imply querying an entire table beforehand, passing all that data around, and then searching through that for a single value.

Is this the only solution or is there a better approach for my scenario? I feel the existing solution of putting an entire table that could potentially have thousands of rows in a variable is redundant and could be optimized.

0 Answers
Related