weixin_33725239 2018-12-07 20:35 采纳率: 0%
浏览 52

提交表单到mysql

I think i have everything right, but I don't see the values in my database. I linked my form correctly to the javascript and i seem to have put the right queries in my code, but it still doesn't work.

My html markup with the form in it, I linked all js in head:

<!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>

My javascript/ link to send form data to mysql database:

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条回答 默认 最新

  • weixin_33719619 2018-12-07 21:17
    关注

    You cannot query a database directly from Javascript (at least not using plain javascript with no libraries).

    Your Javascript code should call a server-side function (that you can write in PHP, C#, etc.), which queries the database and returns the results to your Javascript code.

    This example may be helpful: https://www.w3schools.com/js/js_ajax_database.asp

    评论

报告相同问题?

悬赏问题

  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测