0

I can't use AJAX/PHP solution to upload some data to MySQL database by clicking html button so I created .js function.

upload.js:

function uploadLikes(post) {
// credentials
var pass = require("/home/user/auth.json"); 
var passString = JSON.stringify(pass);  
var passJson = JSON.parse(passString);   

var mysql      = require("mysql");
var connection = mysql.createConnection({
  host     : passJson.bi_anr.host,
  user     : passJson.bi_anr.user,
  password : passJson.bi_anr.password,
});

// source: https://stackoverflow.com/questions/5818312/mysql-with-node-js
connection.connect(function(err) {
  // connected! (unless `err` is set)
});

var query = connection.query("INSERT INTO db.likes (max_hash, ts, likes) VALUES (?, unix_timestamp(now() ), 1 ) ;", post, function(err, result) {
  // Neat!
});
//console.log(query.sql);
}

and according to THIS I created button with javascript execution:

HTML file:

  <html>
  <head>
  <title>Like Button</title>
  </head>

  <body>
  <script src="upload.js"></script>

  <input id="clickMe" type="button" value="clickme"  />
   <script>
      //- Using a function pointer:
      document.getElementById("clickMe").onclick = uploadLikes('TEST');
  </script>       
  </body>
  </html>

but it does not work. JavaScript function is OK, because it works by executing it in terminal using node. Could you help me?

Community
  • 1
  • 1
Taz
  • 5,755
  • 6
  • 26
  • 63

0 Answers0