我想我什么都做对了,但我没有看到数据库中的值。我将我的表单正确地链接到javascript,并且似乎在代码中添加了正确的查询,但是仍然无法工作。
我的html标记和其中的表单,我链接了头中的所有js:
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript sample: Form Validation & Sql insertion</title>
<script src="jquery-3.3.1.min.js"></script>
<script src="databaseManager.js"></script>
<script type="text/javascript" src="js_always_insert.js"></script>
<script type="text/javascript" src="verstuurData.js"></script>
</head>
<body>
<table id="table1">
<tr>
<td>Voornaam</td>
<td><input type="text" id="voor"/></td>
</tr>
<tr>
<td>Achternaam</td>
<td><input type="text" id="achter"/></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" id="email"/></td>
</tr>
<tr>
<td>Wachtwoord</td>
<td><input type="password" id="wachtwoord" onkeyup="verstuurData();"/>
</td>
</tr>
<tr>
<td><input type="button" id="create" value="Create"
onclick="validate();finalValidate();"/></td>
<td><div id="errFinal"></div></td>
</tr>
</table>
</body>
</html>
将表单数据发送到mysql数据库的my javascript/链接:
var dbHost = "-";
var myToken = "-";
mDatabaseManager.connect(dbHost);
mDatabaseManager.authenticate(myToken);
var tableNaam = "persoon";
function verstuurData() {
var persoonGegevens = new Array();
persoonGegevens[0] = document.getElementById('voor').value;
persoonGegevens[1] = document.getElementById('achter').value;
persoonGegevens[2] = document.getElementById('email').value;
persoonGegevens[3] = document.getElementById('wachtwoord').value;
var maakTabel = "CREATE TABLE IF NOT EXISTS " + tableNaam + "(" +
" 'voornaam' varchar(255) DEFAULT NULL, " +
" 'achternaam' varchar(255) DEFAULT NULL, " +
" 'email' varchar(255) DEFAULT NULL, " +
" 'wachtwoord' varchar(255) DEFAULT NULL, ";
mDatabasemanager
.query(maakTabel)
.done(function(verzenden) {
var dataZettenPersoon = "INSERT INTO" + tableNaam + "(" +
"'voornaam' , 'achternaam' , 'email' , 'wachtwoord') " +
" VALUES ('"
+ persoonGegevens[0]
+ "','"
+ persoonGegevens[1]
+ "','"
+ persoonGegevens[2]
+ "','"
+ persoonGegevens[3]
+ "')";
mDatabaseManager
.query(dataZettenPersoon)
.done(function(data) {
})
.fail(function(reason) {
console.log(reason);
});
})
.fail(function(reason) {
console.log(reason);
});
}
1条答案
按热度按时间wgxvkvu91#
不能直接从javascript查询数据库(至少不要使用没有库的纯javascript)。
javascript代码应该调用服务器端函数(可以用php、c#等编写),该函数查询数据库并将结果返回给javascript代码。
此示例可能有帮助:https://www.w3schools.com/js/js_ajax_database.asp