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!!