Zsombocicow Sir
Zsombocicow Sir

Reputation: 15

How to add data to database with node.js&Mysql from online form?

I'm trying to add data to a database with MySQL and node.js by simply clicking the submit button the data should end up in a database. I'm getting an error message that I kinda understand, but I don't have a single clue what to do. I hope some of u might help me out. Not completely finished and simpled down but all the important stuff is in there.

My Form HTML:

<!doctype html>
<html lang="en">
  <head>
    
    <title>Mr.Gutter</title>
   <script src="submitButtonClicked.js"></script>
   
  </head>
  <body>
    
<form>
    <div class="form-group">
        <label for="email">Email address</label>
        <input type="email"  id="email" aria-describedby="emailHelp" placeholder="Enter email" required>
        
   </div>
                
    <center>
    <button type="button"  id ="submitbt"onclick="submitForm()" >Submit</button>
    </center>
</form>


        <script >
       
            //work on it later
            //Openmanual();
        </script>

    
  </body>
</html>

The javascript code for the button:

function submitForm()
{

    
    //get all elements from the form
    console.log('submitForm booting')
    
    var email=document.getElementById("email").value;
    

    console.log('values get....')
    console.log(email)

    dataTable()
}

The dataTabel() function is where it's getting messy.

function dataTable()
{
    var mysql = require('mysql');


    var con = mysql.createConnection({

        host:"localhost",
        user:"root",
        password:"",
        database:"nodedb"

    })

    con.connect(function(err){
        if (err) throw err;
        console.log("Connected to The Database!");
        
        var sql="CREATE TABLE customers (email VARCHAR(255))";
        con.query(sql,function(err,result){
            if (err) throw err;
            console.log("Table has been created!")
        });

    });
}

When the code runs by itself it seems to be working (I mean the table gets created) But when I try to run it on the website I get an error message:

Uncaught ReferenceError: require is not defined
    at dataTable (submitButtonClicked.js:3)
    at submitForm (submitButtonClicked.js:54)
    at HTMLButtonElement.onclick (form.html:379)

(submitButtonClicked.js is the Javascript file and form.html is the form file) I know the problem is with the require('mysql'); part but It works when it runs on its own so I need a solution: How to run it online?

Upvotes: 0

Views: 793

Answers (1)

Evert
Evert

Reputation: 99515

Node.js and your browser both use Javascript. However, they are completely separate environments.

You can't just call a Node.js function from your browser unfortunately.

You will need to build a separate Node.js server. This server does the MySQL queries and should listen for HTTP requests.

You client should call your server using a HTTP client such as fetch. Only when all these things are in place you can achieve what you're hoping to do.

Upvotes: 1

Related Questions