1

Thanks everybody I've solved this problem days ago.But I've edited to delete some specific info in the question(code). So it is not duplicate just revised for protection. I am using nodejs on backend, html on front-end and a database(mysql) to build a web page. And i need a login page for that. My html and nodejs codes are below. There is nothing wrong until the validate() function. My html page should use it, but it doesn't. Nothing changes on html side. Where is my mistake?

NODEJS CODE:

var mysql = require('mysql');   
var express = require('express');
var http = require('http');
var login = express();

//mysql bağlantısı
var db = mysql.createConnection({
    .................});
//connection
    db.connect((err) => {
        if(err){ 
            throw err;}
        else console.log("connected");
    });

//veritabanından bilgi çekimi
    login.get('/', (req,res) => {
        let sql = 'SELECT  `user_name`, `pass` FROM `users`'; 
        db.query(sql,(err,result) => {
            if(err) throw err;
            console.log(result);
            res.sendFile(__dirname + "/login.html");

            var .... = result[0].......; //set user name
            console.log(.);...
            var ......= result[0]......;        //set user pass
            console.log(.....);
        });

    });
    function validate(){
        var username = document.getElementById("....").value;
        var password = document.getElementById("....").value;

        var err=document.getElementById("error").innerHTML;
    //giriş alanları boş kalırsa
        if(username==""){
            alert("Bu gerekli bir alandır");} 
        if(password==""){
            alert("Bu gerekli bir alandır");}
    //username şifre eşleşmesi
        if ( ....== "....." && .....== "...."){ 
            document.getElementById("error").innerHTML="Log in sucessfull"; 
            window.location = "login.html";
            return false;
    }
}

HTML CODE

form id="form_id" action= "/" method="post" name="myform"
input type="text" id="...." placeholder="......" required
input type="....." id="....." placeholder="Password" required
input type="button" id="submit" value="LOGIN" onclick="validate()"

script src="path"
workingonit
  • 23
  • 1
  • 5
  • 1
    That `validate` function should be in your front-end Javascript code, not in the backend. Just because you can use Javascript on the server side doesn't mean you can magically do client-side operations on the server side. (And I don't think the `document` object even exists outside of a browser. `window` certainly doesn't.) – Robin Zigmond Dec 01 '18 at 14:55
  • You need to use [`fetch()`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) (AJAX) in order to call server-side code from the client. For now, I'd call the validate function from your `login.post()` route. –  Dec 01 '18 at 14:59
  • @RobinZigmond I think you are right but I've tried to write the validate() function between script tags on html before. still not working. – workingonit Dec 01 '18 at 15:13

1 Answers1

0

Your are doing front-end validation so your validate funtion must be in front-end in the script tag. call the validation function when the user clicks on the submit buttons.

Another way is doing the validation in backend by express middle-wares. thats back-end validation. check express-validator

defectivepixel
  • 575
  • 1
  • 4
  • 22
  • thanks, but i've already done the first one. – workingonit Dec 01 '18 at 15:20
  • so you define validate() in script tag in front-end and still not working? is there any error in browser console? – defectivepixel Dec 01 '18 at 15:22
  • console error: Uncaught ReferenceError: validate is not defined at HTMLInputElement.onclick – workingonit Dec 01 '18 at 15:30
  • show me your script tag please. – defectivepixel Dec 01 '18 at 15:39
  • – workingonit Dec 01 '18 at 15:41
  • no need for that src attribute and place your script tag on the bottom of body. – defectivepixel Dec 01 '18 at 16:29