I want to make a register form. After the "Submit" button is pressed the script opens my MySql database and inserts a new row in the "Users" table. Everything works, except the browser can't interpret the NodeJs require
function. Some other posts said to use browserify but I want to know if this can be done without other programs.
My code:
register.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
<script src = "index.js" type="text/javascript"></script>
<script src = "register.js" type = "text/javascript"></script>
<link rel="stylesheet" type="text/css" href = "register.css">
</head>
<body>
<form name = "registerForm" method = "post">
<p id = "fillRule">Username must contain lower or uppercase or _! Fill in all boxes except the job! Pass must contain small and uppercase letters and its lenght must be at least 6 char, max 10!</p>
<label for="username">Username:</label>
<input type = "text" id = "username" name = "username" onchange = "checkUser()"> <br/>
<label for="firstname">Firstname:</label>
<input type = "text" id = "firstname" name = "firstame" onchange = "checkFirst()"> <br/>
<label for="lastname">Lastname:</label>
<input type = "text" id = "lastname" name = "lastname" onchange = "checkLast()"> <br/>
<label for="job">Your job:</label>
<input type = "text" id = "job" name = "job" onchange="checkJob()"> <br/>
<label for="pass">Password:</label>
<input type = "text" id = "pass" name = "pass" onchange = "checkPass()"> <br/>
<label for="mail">E-mail:</label>
<input type = "email" id = "mail" name = "mail" onchange = "checkMail()"> <br/>
<input type = "button" value = "send" id = "gomb" disabled = "disabled" onclick = "registerButtonClick()">
</form>
</body>
</html>
index.js
//export {addUser};
// String formatting
if (!String.format) {
String.format = function(format) {
var args = Array.prototype.slice.call(arguments, 1);
return format.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
//
var mysql = require("mysql");
function createConnection(hostName, userName, passwd, db)
{
const con = mysql.createConnection({
host: hostName,
user: userName,
password: passwd,
database: db
});
return con;
}
function openConnection(con)
{
con.connect(function (err) {
if (err) {
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
}
function executeFullSelect(con, tableName)
{
const statement = "SELECT * FROM " + tableName + ";";
con.query(statement, function(err, rows)
{
if (err)
{
throw err;
}
console.log("Data received from " + tableName + ".\n");
console.log(rows);
/*for (var i = 0; i < rows.length; i++)
{
console.log(rows[i].UserName);
}*/
});
}
function insert(con, userName,firstName, lastName, job, mail, passwd)
{
const statement = String.format("call newUser(\"{0}\", \"{1}\", \"{2}\", \"{3}\", \"{4}\", \"{5}\");", userName, firstName, lastName, job, mail, passwd);
con.query(statement, function(err, rows)
{
if (err)
{
console.log("Error at insert");
return 0;
}
console.log(userName + " was successfully added:)");
return 1;
})
}
function closeConnection(con)
{
con.end(function (err) {});
}
function addUser(userName, firstName, lastName, job, mail ,passwd)
{
const con = createConnection("localhost", "root", "micimacko", "users");
openConnection(con);
if (!insert(con, userName, firstName, lastName, job, mail ,passwd))
{
closeConnection(con);
return 0;
}
closeConnection(con);
return 1;
}
register.js
function registerButtonClick()
{
const userName = document.forms["registerForm"]["username"].value;
const firstName = document.forms["registerForm"]["firstname"].value;
const lastName = document.forms["registerForm"]["lastname"].value;
const job = document.forms["registerForm"]["job"].value;
const mail = document.forms["registerForm"]["mail"].value;
const pass = document.forms["registerForm"]["pass"].value;
if (addUser(userName, firstName, lastName, job, mail, pass))
{
alert("added");
}
else
{
alert("failed");
}
}
via Jenei Csilla
No comments:
Post a Comment