我尝试将我的JavaScript文件链接到我的html文件,但什么都没有发生
我制作了一个html文件,我想将它的输入传递到一个单独的JavaScript文件中的函数中,然后让它将其添加到sql server。当我单独运行JavaScript文件时,它可以工作,但当我试图通过单击按钮从html文件调用它时,什么都不会发生。我检查了控制台,没有出现任何错误,我在堆栈溢出上尝试的其他方法也不起作用我尝试将我的JavaScript文件链接到我的html文件,但什么都没有发生,javascript,html,Javascript,Html,我制作了一个html文件,我想将它的输入传递到一个单独的JavaScript文件中的函数中,然后让它将其添加到sql server。当我单独运行JavaScript文件时,它可以工作,但当我试图通过单击按钮从html文件调用它时,什么都不会发生。我检查了控制台,没有出现任何错误,我在堆栈溢出上尝试的其他方法也不起作用 大学注册 身体{ 背景图片:url(“img/SignUp.png”); 背景大小:100%; } h1{ 颜色:#FFFF; 字体系列:富兰克林哥特式中字体、Arial窄字体、
大学注册
身体{
背景图片:url(“img/SignUp.png”);
背景大小:100%;
}
h1{
颜色:#FFFF;
字体系列:富兰克林哥特式中字体、Arial窄字体、Arial无衬线字体;
字号:x大号;
文本对齐:居中;
}
p{
颜色:#FFFF;
字体系列:富兰克林哥特式中字体、Arial窄字体、Arial无衬线字体;
字号:中等;
文本对齐:居中;
}
形式{
文本对齐:居中;
}
大学注册
你们大学的名字是什么
&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp 
此帐户的主要用户的电子邮件是什么
&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp 
你学校的电子邮件url是什么(如果你的学生的电子邮件是example@student.edu放“student.edu”)
&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp 
请输入一个你能记住的密码
&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp&emsp 
提交
我通过在app.js文件中调用该函数来测试它是否有效,但是当我试图通过html文件调用它时,我注释掉了它。我也很难向用户打印错误消息。当我测试这个函数时,我通过打印到控制台来确保它工作正常,但是它说我在示例中看到的函数并没有这样定义。另外,我对JavaScript和html有点陌生,任何清理代码的技巧(比如帮助我将文本框居中,这样我就可以摆脱所有&emsp或减少冗余)也将非常感谢。谢谢
function insertUniversity(name, email, password, url) {
var idk = email.toLowerCase();
var lowurl = url.toLowerCase();
const mysql = require('mysql');
// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
host: 'sample',
user: 'sample',
password: 'password',
database: 'dbo'
});
con.connect((err) => {
if (err) {
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
if (name == null) {
//document.getElementById("error1").innerHTML = "You are missing the name of your university";
console.log("missing info");
con.end((err) => {
// The connection is terminated gracefully
// Ensures all remaining queries are executed
// Then sends a quit packet to the MySQL server.
});
return;
}
if (email == null) {
//document.getElementById("error2").innerHTML = "You are missing a primary email";
console.log("missing info");
con.end((err) => {
// The connection is terminated gracefully
// Ensures all remaining queries are executed
// Then sends a quit packet to the MySQL server.
});
return;
}
if (password == null) {
//document.getElementById("error3").innerHTML = "You are missing a password";
console.log("missing info");
con.end((err) => {
// The connection is terminated gracefully
// Ensures all remaining queries are executed
// Then sends a quit packet to the MySQL server.
});
return;
}
if (url == null) {
//document.getElementById("error4").innerHTML = "You are missing a student url";
console.log("missing info");
con.end((err) => {
// The connection is terminated gracefully
// Ensures all remaining queries are executed
// Then sends a quit packet to the MySQL server.
});
return;
}
con.query(`SELECT * FROM university WHERE university.userId='${name}';`, (err, rows) => {
if (err) throw err;
console.log(rows.length);
if (rows.length > 0) {
con.destroy((err) => {
// The connection is terminated gracefully
// Ensures all remaining queries are executed
// Then sends a quit packet to the MySQL server.
});
return;
} else {
var str = `INSERT INTO university (userID, email, pass, url) VALUES ('${name}', '${idk}', '${password}','${lowurl}');`;
con.query(str, (err, rows) => {
if (err) throw err;
console.log('Data inserted into Db:');
console.log(rows);
});
}
});
}
无法运行在浏览器中发布的代码。为此,您需要一个Node.js服务器
require()
在Vanilla JS中不起作用。将MySQL凭据以明文形式存在也不是一个好主意。任何人都能看到它们
我建议您开始研究Node.js教程。为了限制潜在的问题,我会尝试将
type=“button”
添加到您的提交按钮中。因为它是在表单中,所以它的默认功能是提交表单。在函数中获取名称、电子邮件、密码、url,而不是传递给函数