简体   繁体   中英

Html / Node doesnt saving data in mySql database

I have a button in my following html form. When i click it its supposed to call the javascript insert() function and save to mysql database those 2 variables. But instead of this it just clear all user input fields and realoading the page.Plus its saving nothing ot the database. What im doing wrong?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="css/PassHomeStyle.css">
    <title>Document</title>
</head>
<body>
    <div class="container">  
        <form id="contact"   method="post">
          <h3>Submit your data</h3>

          <fieldset>
            <input placeholder="Website" name="" type="text" tabindex="1" required autofocus>
          </fieldset>
          <fieldset>
            <input placeholder="Your Email Address" name="email" type="email" tabindex="2" required>
          </fieldset>
          <fieldset>
            <input placeholder="Your Password" name="password" type="text"  tabindex="3" required>
          </fieldset>
            <button name="submit" type="submit" id="contact-submit" onclick="insert()" data-submit="...Sending">Submit</button>
          </fieldset>

        </form>
 </div>
</body>
</html>
<script>
  function insert(){
        var mysql = require('mysql');

 var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database: "assessment"
 });

 con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
  var sql = "INSERT INTO users (username, password) VALUES ('james', 'bond')";
  con.query(sql, function (err, result) {
    if (err) throw err;
    console.log("1 record inserted");

  });
});
}
 </script>

Pass e in insert like insert(e) then Write e.preventDefault(); It prevents default behaviour after clicking submit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM