0

This is my form:

<form action="index.php" method="post">
  <div class="alert alert-error" id="reg-error"></div>
  <div class="form-group" >
    <label for="reg=name">Username: </label>
    <input type="text" name="reg-name" id="reg-name" class="form-control" required placeholder="namerinho">
  </div>
  <div class="form-group" >
    <label for="reg-email">E-mail:</label>
    <input type="email" name="reg-email" id="reg-email" class="form-control" required placeholder="surnaminho">
  </div>
  <div class="form-group" >
    <label for="reg-pass">Password:</label>
    <input type="password" name="reg-pass" class="form-control" required placeholder="******">
  </div>
  <div class="form-group" >
    <label for="reg-pass2">Confirm Password:</label>
    <input type="password" name="reg-pass2" class="form-control" required placeholder="******">
  </div>
  <button class="btn btn-default" name="Register-btn" id="Register-btn" type="button">Register</button> 
</form> 

html file has link to script.js This is my script.js (so far):

$(document).ready(function(){
  $( "#abv" ).click(function() {
    $.post('script/register'); // here I expect to get return true or false
  });
});

and this is register code to which i make http request:

var userOrEmailExists = false;
var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  port     : 3307,
  user     : 'root',
  password : 'root',
  database : 'database'
});

connection.query('select email, username from users',function(err, rows) {
  if (err) {
    console.error('error connecting: ' + err.stack);
    return;
  }
  var users;
  users = rows;
  var c = 0;
  var x = 'asd';
  for (var i = users.length - 1; i >= 0; i--) {
    if (user === users[i].username || email ===users[i].email){
        userOrEmailExists = true;
    }
  }
});
// if(userOrEmailExists === true){// I will insert data here}

connection.end();

return userOrEmailExists;

And as I planned on Post I will pass input values to register.js and there I will check if that kind of user exists or not and then either insert or not. I am not sure if this is a good idea to do that, but as I planned everything will done on button click(and register.js will return true or false) So again, just to be clear, My question is how to pass variables from script.js to register.js and then return a boolean value and do something with it in script js. Thank you!!

Kristian Barrett
  • 3,574
  • 2
  • 26
  • 40
enigmq
  • 393
  • 5
  • 19
  • I guess its something with module.exports, but still can't figure it out. – enigmq Dec 24 '16 at 16:24
  • But still cant figure it out how to do that without using me using terminal and calling that file by myself... – enigmq Dec 24 '16 at 16:26
  • Probably a dup of [How do I return a value from an asynchronous function](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). `connection.query()` is async and finishes long after your function has already returned. So, you can't do `return userOrEmailExists;` because `userOrEmailExists` has not yet been set when your function returns. – jfriend00 Dec 24 '16 at 17:46
  • Thanks you very much. Link you gave me is very useful :) – enigmq Dec 24 '16 at 19:26

0 Answers0